Skip to content

Commit 0ae3480

Browse files
authored
[Float] Suspend unstyled content for up to 1 minute (#26532)
We almost never want to show content before its styles have loaded. But eventually we will give up and allow unstyled content. So this extends the timeout to a full minute. This somewhat arbitrary — big enough that you'd only reach it under extreme circumstances. Note that, like regular Suspense, the app is still interactive while we're waiting for content to load. Only the unstyled content is blocked from appearing, not updates in general. A new update will interrupt it. We should figure out what the browser engines do during initial page load and consider aligning our behavior with that. It's supposed to be render blocking by default but there may be some cases where they, too, give up and FOUC.
1 parent 8888746 commit 0ae3480

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

packages/react-dom-bindings/src/client/ReactDOMHostConfig.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,28 +3333,34 @@ export function waitForCommitToBeReady(): null | (Function => Function) {
33333333
// tasks to wait on.
33343334
if (state.count > 0) {
33353335
return commit => {
3336-
unsuspendAfterTimeout(state);
3336+
// We almost never want to show content before its styles have loaded. But
3337+
// eventually we will give up and allow unstyled content. So this number is
3338+
// somewhat arbitrary — big enough that you'd only reach it under
3339+
// extreme circumstances.
3340+
// TODO: Figure out what the browser engines do during initial page load and
3341+
// consider aligning our behavior with that.
3342+
const stylesheetTimer = setTimeout(() => {
3343+
if (state.stylesheets) {
3344+
insertSuspendedStylesheets(state, state.stylesheets);
3345+
}
3346+
if (state.unsuspend) {
3347+
const unsuspend = state.unsuspend;
3348+
state.unsuspend = null;
3349+
unsuspend();
3350+
}
3351+
}, 60000); // one minute
3352+
33373353
state.unsuspend = commit;
33383354

3339-
return () => (state.unsuspend = null);
3355+
return () => {
3356+
state.unsuspend = null;
3357+
clearTimeout(stylesheetTimer);
3358+
};
33403359
};
33413360
}
33423361
return null;
33433362
}
33443363

3345-
function unsuspendAfterTimeout(state: SuspendedState) {
3346-
setTimeout(() => {
3347-
if (state.stylesheets) {
3348-
insertSuspendedStylesheets(state, state.stylesheets);
3349-
}
3350-
if (state.unsuspend) {
3351-
const unsuspend = state.unsuspend;
3352-
state.unsuspend = null;
3353-
unsuspend();
3354-
}
3355-
}, 500);
3356-
}
3357-
33583364
function onUnsuspend(this: SuspendedState) {
33593365
this.count--;
33603366
if (this.count === 0) {

packages/react-dom/src/__tests__/ReactDOMFloat-test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3163,7 +3163,7 @@ body {
31633163
);
31643164
});
31653165

3166-
it('can unsuspend after a timeout even if some assets never load', async () => {
3166+
it('stylesheets block render, with a really long timeout', async () => {
31673167
function App({children}) {
31683168
return (
31693169
<html>
@@ -3191,7 +3191,22 @@ body {
31913191
</html>,
31923192
);
31933193

3194-
jest.advanceTimersByTime(1000);
3194+
// Advance time by 50 seconds. Even still, the transition is suspended.
3195+
jest.advanceTimersByTime(50000);
3196+
await waitForAll([]);
3197+
expect(getMeaningfulChildren(document)).toEqual(
3198+
<html>
3199+
<head>
3200+
<link rel="preload" href="foo" as="style" />
3201+
</head>
3202+
<body />
3203+
</html>,
3204+
);
3205+
3206+
// Advance time by 10 seconds more. A full minute total has elapsed. At this
3207+
// point, something must have really gone wrong, so we time out and allow
3208+
// unstyled content to be displayed.
3209+
jest.advanceTimersByTime(10000);
31953210
expect(getMeaningfulChildren(document)).toEqual(
31963211
<html>
31973212
<head>

0 commit comments

Comments
 (0)