-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Client render dehydrated Suspense boundaries on document load #31620
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const JSDOMModule = jest.requireActual('jsdom'); | ||
|
||
const OriginalJSDOM = JSDOMModule.JSDOM; | ||
|
||
module.exports = JSDOMModule; | ||
module.exports.JSDOM = function JSDOM() { | ||
let result; | ||
if (new.target) { | ||
result = Reflect.construct(OriginalJSDOM, arguments); | ||
} else { | ||
result = JSDOM.apply(undefined, arguments); | ||
} | ||
|
||
require('./ReactJSDOMUtils').setupDocumentReadyState( | ||
result.window.document, | ||
result.window.Event, | ||
); | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export function setupDocumentReadyState( | ||
document: Document, | ||
Event: typeof Event, | ||
) { | ||
let readyState: 0 | 1 | 2 = 0; | ||
Object.defineProperty(document, 'readyState', { | ||
get() { | ||
switch (readyState) { | ||
case 0: | ||
return 'loading'; | ||
case 1: | ||
return 'interactive'; | ||
case 2: | ||
return 'complete'; | ||
} | ||
}, | ||
set(value) { | ||
if (value === 'interactive' && readyState < 1) { | ||
readyState = 1; | ||
document.dispatchEvent(new Event('readystatechange')); | ||
} else if (value === 'complete' && readyState < 2) { | ||
readyState = 2; | ||
document.dispatchEvent(new Event('readystatechange')); | ||
document.dispatchEvent(new Event('DOMContentLoaded')); | ||
} else if (value === 'loading') { | ||
// We allow resetting the readyState to loading mostly for pragamtism. | ||
// tests that use this environment don't reset the document between tests. | ||
readyState = 0; | ||
} | ||
}, | ||
configurable: true, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,6 +194,8 @@ const SUSPENSE_FALLBACK_START_DATA = '$!'; | |
const FORM_STATE_IS_MATCHING = 'F!'; | ||
const FORM_STATE_IS_NOT_MATCHING = 'F'; | ||
|
||
const DOCUMENT_READY_STATE_COMPLETE = 'complete'; | ||
|
||
const STYLE = 'style'; | ||
|
||
opaque type HostContextNamespace = 0 | 1 | 2; | ||
|
@@ -1262,7 +1264,11 @@ export function isSuspenseInstancePending(instance: SuspenseInstance): boolean { | |
export function isSuspenseInstanceFallback( | ||
instance: SuspenseInstance, | ||
): boolean { | ||
return instance.data === SUSPENSE_FALLBACK_START_DATA; | ||
return ( | ||
instance.data === SUSPENSE_FALLBACK_START_DATA || | ||
(instance.data === SUSPENSE_PENDING_START_DATA && | ||
instance.ownerDocument.readyState === DOCUMENT_READY_STATE_COMPLETE) | ||
); | ||
} | ||
|
||
export function getSuspenseInstanceFallbackErrorDetails( | ||
|
@@ -1303,6 +1309,20 @@ export function registerSuspenseInstanceRetry( | |
instance: SuspenseInstance, | ||
callback: () => void, | ||
) { | ||
const ownerDocument = instance.ownerDocument; | ||
if (ownerDocument.readyState !== DOCUMENT_READY_STATE_COMPLETE) { | ||
ownerDocument.addEventListener( | ||
'DOMContentLoaded', | ||
() => { | ||
if (instance.data === SUSPENSE_PENDING_START_DATA) { | ||
callback(); | ||
} | ||
}, | ||
{ | ||
once: true, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’s probably fine to rely on this these days but I’m always paranoid of new features. If this is an old browser it would forever leak potentially very large trees. An alternative to this is to explicitly just remove the same listener when called. |
||
); | ||
} | ||
instance._reactRetry = callback; | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whenever we add listeners in the render phase I need to look closer because it’s risky. For example that it won’t ever clean up.
We already do this with the retry but at least that overrides an existing callback which frees the previous one and it automatically frees once the dom node itself is removed.
For every attempt that has to be given up, we end up closing over the whole fiber tree that was attempted that we normally just gc. The clean up mechanism we have as a safety for leaking large trees won’t even kick in because it never commits so there’s no unmount. (This would be fixed by the big refactor idea to split the Fibers into two but unclear if that will ever happen.)
So it’s absolutely critical that this event actually fires and the closure gets cleaned up. However even then it might leak some attempted trees in the meantime which can leak to more gc work too since they now move to older generations.
The ideal would be to either move this whole thing to the commit phase or maybe lazily scan the fiber tree.