Skip to content

Commit 16d2bbb

Browse files
authoredDec 3, 2024··
Client render dehydrated Suspense boundaries on document load (#31620)
When streaming SSR while hydrating React will wait for Suspense boundaries to be revealed by the SSR stream before attempting to hydrate them. The rationale here is that the Server render is likely further ahead of whatever the client would produce so waiting to let the server stream in the UI is preferable to retrying on the client and possibly delaying how quickly the primary content becomes available. However If the connection closes early (user hits stop for instance) or there is a server error which prevents additional HTML from being delivered to the client this can put React into a broken state where the boundary never resolves nor errors and the hydration never retries that boundary freezing it in it's fallback state. Once the document has fully loaded we know there is not way any additional Suspense boundaries can arrive. This update changes react-dom on the client to schedule client renders for any unfinished Suspense boundaries upon document loading. The technique for client rendering a fallback is pretty straight forward. When hydrating a Suspense boundary if the Document is in 'complete' readyState we interpret pending boundaries as fallback boundaries. If the readyState is not 'complete' we register an event to retry the boundary when the DOMContentLoaded event fires. To test this I needed JSDOM to model readyState. We previously had a temporary implementation of readyState for SSR streaming but I ended up implementing this as a mock of JSDOM that implements a fake readyState that is mutable. It starts off in 'loading' readyState and you can advance it by mutating document.readyState. You can also reset it to 'loading'. It fires events when changing states. This seems like the least invasive way to get closer-to-real-browser behavior in a way that won't require remembering this subtle detail every time you create a test that asserts Suspense resolution order.
1 parent 6bcf0d2 commit 16d2bbb

File tree

10 files changed

+445
-224
lines changed

10 files changed

+445
-224
lines changed
 

Diff for: ‎packages/internal-test-utils/ReactJSDOM.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const JSDOMModule = jest.requireActual('jsdom');
2+
3+
const OriginalJSDOM = JSDOMModule.JSDOM;
4+
5+
module.exports = JSDOMModule;
6+
module.exports.JSDOM = function JSDOM() {
7+
let result;
8+
if (new.target) {
9+
result = Reflect.construct(OriginalJSDOM, arguments);
10+
} else {
11+
result = JSDOM.apply(undefined, arguments);
12+
}
13+
14+
require('./ReactJSDOMUtils').setupDocumentReadyState(
15+
result.window.document,
16+
result.window.Event,
17+
);
18+
19+
return result;
20+
};

Diff for: ‎packages/internal-test-utils/ReactJSDOMUtils.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export function setupDocumentReadyState(
2+
document: Document,
3+
Event: typeof Event,
4+
) {
5+
let readyState: 0 | 1 | 2 = 0;
6+
Object.defineProperty(document, 'readyState', {
7+
get() {
8+
switch (readyState) {
9+
case 0:
10+
return 'loading';
11+
case 1:
12+
return 'interactive';
13+
case 2:
14+
return 'complete';
15+
}
16+
},
17+
set(value) {
18+
if (value === 'interactive' && readyState < 1) {
19+
readyState = 1;
20+
document.dispatchEvent(new Event('readystatechange'));
21+
} else if (value === 'complete' && readyState < 2) {
22+
readyState = 2;
23+
document.dispatchEvent(new Event('readystatechange'));
24+
document.dispatchEvent(new Event('DOMContentLoaded'));
25+
} else if (value === 'loading') {
26+
// We allow resetting the readyState to loading mostly for pragamtism.
27+
// tests that use this environment don't reset the document between tests.
28+
readyState = 0;
29+
}
30+
},
31+
configurable: true,
32+
});
33+
}

Diff for: ‎packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ const SUSPENSE_FALLBACK_START_DATA = '$!';
194194
const FORM_STATE_IS_MATCHING = 'F!';
195195
const FORM_STATE_IS_NOT_MATCHING = 'F';
196196

197+
const DOCUMENT_READY_STATE_COMPLETE = 'complete';
198+
197199
const STYLE = 'style';
198200

199201
opaque type HostContextNamespace = 0 | 1 | 2;
@@ -1262,7 +1264,11 @@ export function isSuspenseInstancePending(instance: SuspenseInstance): boolean {
12621264
export function isSuspenseInstanceFallback(
12631265
instance: SuspenseInstance,
12641266
): boolean {
1265-
return instance.data === SUSPENSE_FALLBACK_START_DATA;
1267+
return (
1268+
instance.data === SUSPENSE_FALLBACK_START_DATA ||
1269+
(instance.data === SUSPENSE_PENDING_START_DATA &&
1270+
instance.ownerDocument.readyState === DOCUMENT_READY_STATE_COMPLETE)
1271+
);
12661272
}
12671273

12681274
export function getSuspenseInstanceFallbackErrorDetails(
@@ -1303,6 +1309,20 @@ export function registerSuspenseInstanceRetry(
13031309
instance: SuspenseInstance,
13041310
callback: () => void,
13051311
) {
1312+
const ownerDocument = instance.ownerDocument;
1313+
if (ownerDocument.readyState !== DOCUMENT_READY_STATE_COMPLETE) {
1314+
ownerDocument.addEventListener(
1315+
'DOMContentLoaded',
1316+
() => {
1317+
if (instance.data === SUSPENSE_PENDING_START_DATA) {
1318+
callback();
1319+
}
1320+
},
1321+
{
1322+
once: true,
1323+
},
1324+
);
1325+
}
13061326
instance._reactRetry = callback;
13071327
}
13081328

0 commit comments

Comments
 (0)
Please sign in to comment.