Skip to content

Commit

Permalink
[Fiber] render boundary in fallback if it contains a new stylesheet d…
Browse files Browse the repository at this point in the history
…uring sync update

When we implemented Suspensey CSS we had a heuristic that if the update was sync we would ignore the loading states of any new stylesheets and just do the commit. But for a stylesheet capability to be useful it needs to reliably prevent FOUC and since the stylesheet api is opt-in through precedence we don't have to maintain backaward compat (old stylesheets do not block commit but then nobody really renders them because of FOUC anyway)

This update modifies the logic to put a boundary back into fallback if a sync update would lead to a stylesheet commiting before it loaded.
  • Loading branch information
gnoff committed May 21, 2024
1 parent 8f3c052 commit 8596091
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 82 deletions.
6 changes: 3 additions & 3 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -3154,7 +3154,6 @@ export function mayResourceSuspendCommit(resource: Resource): boolean {
}

export function preloadInstance(type: Type, props: Props): boolean {
// Return true to indicate it's already loaded
return true;
}

Expand All @@ -3163,10 +3162,11 @@ export function preloadResource(resource: Resource): boolean {
resource.type === 'stylesheet' &&
(resource.state.loading & Settled) === NotLoaded
) {
// we have not finished loading the underlying stylesheet yet.
// Return false to indicate this resource should suspend
return false;
}
// Return true to indicate it's already loaded

// Return true to indicate this resource should not suspend
return true;
}

Expand Down
142 changes: 125 additions & 17 deletions packages/react-dom/src/__tests__/ReactDOMFloat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2946,25 +2946,8 @@ body {
<link rel="preload" as="style" href="bar" />,
]);

// Try just this and crash all of Jest
errorStylesheets(['bar']);

// // Try this and it fails the test when it shouldn't
// await act(() => {
// errorStylesheets(['bar']);
// });

// // Try this there is nothing throwing here which is not really surprising since
// // the error is bubbling up through some kind of unhandled promise rejection thingy but
// // still I thought it was worth confirming
// try {
// await act(() => {
// errorStylesheets(['bar']);
// });
// } catch (e) {
// console.log(e);
// }

loadStylesheets(['foo']);
assertLog(['load stylesheet: foo', 'error stylesheet: bar']);

Expand Down Expand Up @@ -3197,6 +3180,131 @@ body {
);
});

it('will put a Suspense boundary into fallback if it contains a stylesheet not loaded during a sync update', async () => {
function App({children}) {
return (
<html>
<body>{children}</body>
</html>
);
}
const root = ReactDOMClient.createRoot(document);

await clientAct(() => {
root.render(<App />);
});
await waitForAll([]);

await clientAct(() => {
root.render(
<App>
<Suspense fallback="loading...">
<div>
hello
<link rel="stylesheet" href="foo" precedence="default" />
</div>
</Suspense>
</App>,
);
});
await waitForAll([]);

// Although the commit suspended, a preload was inserted.
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link rel="preload" href="foo" as="style" />
</head>
<body>loading...</body>
</html>,
);

loadPreloads(['foo']);
assertLog(['load preload: foo']);
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link rel="stylesheet" href="foo" data-precedence="default" />
<link rel="preload" href="foo" as="style" />
</head>
<body>loading...</body>
</html>,
);

loadStylesheets(['foo']);
assertLog(['load stylesheet: foo']);
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link rel="stylesheet" href="foo" data-precedence="default" />
<link rel="preload" href="foo" as="style" />
</head>
<body>
<div>hello</div>
</body>
</html>,
);

await clientAct(() => {
root.render(
<App>
<Suspense fallback="loading...">
<div>
hello
<link rel="stylesheet" href="foo" precedence="default" />
<link rel="stylesheet" href="bar" precedence="default" />
</div>
</Suspense>
</App>,
);
});
await waitForAll([]);
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link rel="stylesheet" href="foo" data-precedence="default" />
<link rel="preload" href="foo" as="style" />
<link rel="preload" href="bar" as="style" />
</head>
<body>
<div style="display: none;">hello</div>loading...
</body>
</html>,
);

loadPreloads(['bar']);
assertLog(['load preload: bar']);
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link rel="stylesheet" href="foo" data-precedence="default" />
<link rel="stylesheet" href="bar" data-precedence="default" />
<link rel="preload" href="foo" as="style" />
<link rel="preload" href="bar" as="style" />
</head>
<body>
<div style="display: none;">hello</div>loading...
</body>
</html>,
);

loadStylesheets(['bar']);
assertLog(['load stylesheet: bar']);
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link rel="stylesheet" href="foo" data-precedence="default" />
<link rel="stylesheet" href="bar" data-precedence="default" />
<link rel="preload" href="foo" as="style" />
<link rel="preload" href="bar" as="style" />
</head>
<body>
<div style="">hello</div>
</body>
</html>,
);
});

it('can suspend commits on more than one root for the same resource at the same time', async () => {
document.body.innerHTML = '';
const container1 = document.createElement('div');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ export function maySuspendCommit(type: Type, props: Props): boolean {
}

export function preloadInstance(type: Type, props: Props): boolean {
// Return true to indicate it's already loaded
// Return false to indicate it's already loaded
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,12 +611,11 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
}
return false;
} else {
// If this is false, React will trigger a fallback, if needed.
return record.status === 'fulfilled';
}
},

preloadResource(resource: mixed): boolean {
preloadResource(resource: mixed): number {
throw new Error(
'Resources are not implemented for React Noop yet. This method should not be called',
);
Expand Down
63 changes: 15 additions & 48 deletions packages/react-reconciler/src/ReactFiberCompleteWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ import {
getRenderTargetTime,
getWorkInProgressTransitions,
shouldRemainOnPreviousScreen,
getWorkInProgressRootRenderLanes,
} from './ReactFiberWorkLoop';
import {
OffscreenLane,
Expand All @@ -161,7 +160,6 @@ import {
includesSomeLane,
mergeLanes,
claimNextRetryLane,
includesOnlyNonUrgentLanes,
} from './ReactFiberLane';
import {resetChildFibers} from './ReactChildFiber';
import {createScopeInstance} from './ReactFiberScope';
Expand Down Expand Up @@ -534,41 +532,15 @@ function preloadInstanceAndSuspendIfNeeded(
// loaded yet.
workInProgress.flags |= MaySuspendCommit;

// Check if we're rendering at a "non-urgent" priority. This is the same
// check that `useDeferredValue` does to determine whether it needs to
// defer. This is partly for gradual adoption purposes (i.e. shouldn't start
// suspending until you opt in with startTransition or Suspense) but it
// also happens to be the desired behavior for the concrete use cases we've
// thought of so far, like CSS loading, fonts, images, etc.
//
// We check the "root" render lanes here rather than the "subtree" render
// because during a retry or offscreen prerender, the "subtree" render
// lanes may include additional "base" lanes that were deferred during
// a previous render.
// TODO: We may decide to expose a way to force a fallback even during a
// sync update.
const rootRenderLanes = getWorkInProgressRootRenderLanes();
if (!includesOnlyNonUrgentLanes(rootRenderLanes)) {
// This is an urgent render. Don't suspend or show a fallback. Also,
// there's no need to preload, because we're going to commit this
// synchronously anyway.
// TODO: Could there be benefit to preloading even during a synchronous
// render? The main thread will be blocked until the commit phase, but
// maybe the browser would be able to start loading off thread anyway?
// Likely a micro-optimization either way because typically new content
// is loaded during a transition, not an urgent render.
} else {
// Preload the instance
const isReady = preloadInstance(type, props);
if (!isReady) {
if (shouldRemainOnPreviousScreen()) {
// It's OK to suspend. Mark the fiber so we know to suspend before the
// commit phase. Then continue rendering.
workInProgress.flags |= ShouldSuspendCommit;
} else {
// Trigger a fallback rather than block the render.
suspendCommit();
}
// preload the instance if necessary. Even if this is an urgent render there
// could be benefits to preloading early.
// @TODO we should probably do the preload in begin work
const isReady = preloadInstance(type, props);
if (!isReady) {
if (shouldRemainOnPreviousScreen()) {
workInProgress.flags |= ShouldSuspendCommit;
} else {
suspendCommit();
}
}
}
Expand All @@ -588,17 +560,12 @@ function preloadResourceAndSuspendIfNeeded(

workInProgress.flags |= MaySuspendCommit;

const rootRenderLanes = getWorkInProgressRootRenderLanes();
if (!includesOnlyNonUrgentLanes(rootRenderLanes)) {
// This is an urgent render. Don't suspend or show a fallback.
} else {
const isReady = preloadResource(resource);
if (!isReady) {
if (shouldRemainOnPreviousScreen()) {
workInProgress.flags |= ShouldSuspendCommit;
} else {
suspendCommit();
}
const isReady = preloadResource(resource);
if (!isReady) {
if (shouldRemainOnPreviousScreen()) {
workInProgress.flags |= ShouldSuspendCommit;
} else {
suspendCommit();
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions packages/react-reconciler/src/ReactFiberWorkLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
} from './ReactFiberTracingMarkerComponent';
import type {OffscreenInstance} from './ReactFiberActivityComponent';
import type {RenderTaskFn} from './ReactFiberRootScheduler';
import type {Resource} from './ReactFiberConfig';

import {
enableCreateEventHandleAPI,
Expand Down Expand Up @@ -75,6 +76,7 @@ import {
startSuspendingCommit,
waitForCommitToBeReady,
preloadInstance,
preloadResource,
supportsHydration,
setCurrentUpdatePriority,
getCurrentUpdatePriority,
Expand Down Expand Up @@ -2219,9 +2221,13 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
break;
}
case SuspendedOnInstanceAndReadyToContinue: {
let resource: null | Resource = null;
switch (workInProgress.tag) {
case HostHoistable: {
resource = workInProgress.memoizedState;
}
// intentional fallthrough
case HostComponent:
case HostHoistable:
case HostSingleton: {
// Before unwinding the stack, check one more time if the
// instance is ready. It may have loaded when React yielded to
Expand All @@ -2232,7 +2238,9 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
const hostFiber = workInProgress;
const type = hostFiber.type;
const props = hostFiber.pendingProps;
const isReady = preloadInstance(type, props);
const isReady = resource
? preloadResource(resource)
: preloadInstance(type, props);
if (isReady) {
// The data resolved. Resume the work loop as if nothing
// suspended. Unlike when a user component suspends, we don't
Expand Down

0 comments on commit 8596091

Please sign in to comment.