Skip to content

Commit c2857fe

Browse files
committed
Prerendering should not cancel a pending commit
If there's a pending commit that's expected to run within an short amount of time, we should not cancel it in favor of prerendering. We should wait for the commit to finish before prerendering. This does not apply to commits that are suspended indefinitely, like when you suspend outside of a Suspense boundary, or in the shell during a transition. Because those cases do not represent a complete tree. There's one special case that we intentionally (for now) don't handle, which is Suspensey CSS. These are also expected to resolve quickly, because of preloading, but theoretically they could block forever like in a normal "suspend indefinitely" scenario. In the future, we should consider only blocking for up to some time limit before discarding the commit in favor of prerendering.
1 parent dc3ccc2 commit c2857fe

14 files changed

+384
-256
lines changed

packages/react-cache/src/__tests__/ReactCacheOld-test.internal.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,12 @@ describe('ReactCache', () => {
206206
...(gate('enableSiblingPrerendering') ? ['Invalid key type'] : []),
207207
]);
208208
} else {
209-
await waitForAll(['App', 'Loading...']);
209+
await waitForAll([
210+
'App',
211+
'Loading...',
212+
213+
...(gate('enableSiblingPrerendering') ? ['App'] : []),
214+
]);
210215
}
211216
});
212217

@@ -226,10 +231,14 @@ describe('ReactCache', () => {
226231
await waitForPaint(['Suspend! [1]', 'Loading...']);
227232
jest.advanceTimersByTime(100);
228233
assertLog(['Promise resolved [1]']);
229-
await waitForAll([1, 'Suspend! [2]', 1, 'Suspend! [2]', 'Suspend! [3]']);
234+
await waitForAll([1, 'Suspend! [2]']);
235+
236+
jest.advanceTimersByTime(100);
237+
assertLog(['Promise resolved [2]']);
238+
await waitForAll([1, 2, 'Suspend! [3]']);
230239

231240
jest.advanceTimersByTime(100);
232-
assertLog(['Promise resolved [2]', 'Promise resolved [3]']);
241+
assertLog(['Promise resolved [3]']);
233242
await waitForAll([1, 2, 3]);
234243

235244
await act(() => jest.advanceTimersByTime(100));

packages/react-reconciler/src/ReactFiberLane.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,29 @@ export function getNextLanes(root: FiberRoot, wipLanes: Lanes): Lanes {
233233
const pingedLanes = root.pingedLanes;
234234
const warmLanes = root.warmLanes;
235235

236+
// finishedLanes represents a completed tree that is ready to commit.
237+
//
238+
// It's not worth doing discarding the completed tree in favor of performing
239+
// speculative work. So always check this before deciding to warm up
240+
// the siblings.
241+
//
242+
// Note that this is not set in a "suspend indefinitely" scenario, like when
243+
// suspending outside of a Suspense boundary, or in the shell during a
244+
// transition — only in cases where we are very likely to commit the tree in
245+
// a brief amount of time (i.e. below the "Just Noticeable Difference"
246+
// threshold).
247+
//
248+
// TODO: finishedLanes is also set when a Suspensey resource, like CSS or
249+
// images, suspends during the commit phase. (We could detect that here by
250+
// checking for root.cancelPendingCommit.) These are also expected to resolve
251+
// quickly, because of preloading, but theoretically they could block forever
252+
// like in a normal "suspend indefinitely" scenario. In the future, we should
253+
// consider only blocking for up to some time limit before discarding the
254+
// commit in favor of prerendering. If we do discard a pending commit, then
255+
// the commit phase callback should act as a ping to try the original
256+
// render again.
257+
const rootHasPendingCommit = root.finishedLanes !== NoLanes;
258+
236259
// Do not work on any idle work until all the non-idle work has finished,
237260
// even if the work is suspended.
238261
const nonIdlePendingLanes = pendingLanes & NonIdleLanes;
@@ -248,9 +271,11 @@ export function getNextLanes(root: FiberRoot, wipLanes: Lanes): Lanes {
248271
nextLanes = getHighestPriorityLanes(nonIdlePingedLanes);
249272
} else {
250273
// Nothing has been pinged. Check for lanes that need to be prewarmed.
251-
const lanesToPrewarm = nonIdlePendingLanes & ~warmLanes;
252-
if (lanesToPrewarm !== NoLanes) {
253-
nextLanes = getHighestPriorityLanes(lanesToPrewarm);
274+
if (!rootHasPendingCommit) {
275+
const lanesToPrewarm = nonIdlePendingLanes & ~warmLanes;
276+
if (lanesToPrewarm !== NoLanes) {
277+
nextLanes = getHighestPriorityLanes(lanesToPrewarm);
278+
}
254279
}
255280
}
256281
}
@@ -270,9 +295,11 @@ export function getNextLanes(root: FiberRoot, wipLanes: Lanes): Lanes {
270295
nextLanes = getHighestPriorityLanes(pingedLanes);
271296
} else {
272297
// Nothing has been pinged. Check for lanes that need to be prewarmed.
273-
const lanesToPrewarm = pendingLanes & ~warmLanes;
274-
if (lanesToPrewarm !== NoLanes) {
275-
nextLanes = getHighestPriorityLanes(lanesToPrewarm);
298+
if (!rootHasPendingCommit) {
299+
const lanesToPrewarm = pendingLanes & ~warmLanes;
300+
if (lanesToPrewarm !== NoLanes) {
301+
nextLanes = getHighestPriorityLanes(lanesToPrewarm);
302+
}
276303
}
277304
}
278305
}

packages/react-reconciler/src/ReactFiberWorkLoop.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -997,8 +997,6 @@ export function performConcurrentWorkOnRoot(
997997

998998
// We now have a consistent tree. The next step is either to commit it,
999999
// or, if something suspended, wait to commit it after a timeout.
1000-
root.finishedWork = finishedWork;
1001-
root.finishedLanes = lanes;
10021000
finishConcurrentRender(root, exitStatus, finishedWork, lanes);
10031001
}
10041002
break;
@@ -1142,6 +1140,12 @@ function finishConcurrentRender(
11421140
}
11431141
}
11441142

1143+
// Only set these if we have a complete tree that is ready to be committed.
1144+
// We use these fields to determine later whether or not the work should be
1145+
// discarded for a fresh render attempt.
1146+
root.finishedWork = finishedWork;
1147+
root.finishedLanes = lanes;
1148+
11451149
if (shouldForceFlushFallbacksInDEV()) {
11461150
// We're inside an `act` scope. Commit immediately.
11471151
commitRoot(
@@ -1174,8 +1178,11 @@ function finishConcurrentRender(
11741178

11751179
const nextLanes = getNextLanes(root, NoLanes);
11761180
if (nextLanes !== NoLanes) {
1177-
// There's additional work we can do on this root. We might as well
1178-
// attempt to work on that while we're suspended.
1181+
// There are additional updates we can do on this root. We might as
1182+
// well attempt to work on that while we're suspended.
1183+
//
1184+
// Notably, this bailout does not occur if the only thing remaining is
1185+
// Suspense retries.
11791186
return;
11801187
}
11811188

@@ -2226,6 +2233,14 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
22262233
workInProgressTransitions = getTransitionsForLanes(root, lanes);
22272234
resetRenderTimer();
22282235
prepareFreshStack(root, lanes);
2236+
} else {
2237+
// This is a continuation of an existing work-in-progress.
2238+
//
2239+
// If we were previously in prerendering mode, check if we received any new
2240+
// data during an interleaved event.
2241+
if (workInProgressRootIsPrerendering) {
2242+
workInProgressRootIsPrerendering = checkIfRootIsPrerendering(root, lanes);
2243+
}
22292244
}
22302245

22312246
if (__DEV__) {

packages/react-reconciler/src/__tests__/DebugTracing-test.internal.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,26 @@ describe('DebugTracing', () => {
187187
`group: ⚛ render (${DEFAULT_LANE_STRING})`,
188188
'log: ⚛ Example suspended',
189189
`groupEnd: ⚛ render (${DEFAULT_LANE_STRING})`,
190+
191+
...(gate('enableSiblingPrerendering')
192+
? [
193+
`group: ⚛ render (${RETRY_LANE_STRING})`,
194+
'log: ⚛ Example suspended',
195+
`groupEnd: ⚛ render (${RETRY_LANE_STRING})`,
196+
]
197+
: []),
190198
]);
191199

192200
logs.splice(0);
193201

194202
await act(async () => await resolveFakeSuspensePromise());
195-
expect(logs).toEqual(['log: ⚛ Example resolved']);
203+
expect(logs).toEqual([
204+
'log: ⚛ Example resolved',
205+
206+
...(gate('enableSiblingPrerendering')
207+
? ['log: ⚛ Example resolved']
208+
: []),
209+
]);
196210
});
197211

198212
// @gate experimental && build === 'development' && enableDebugTracing && enableCPUSuspense

packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4207,13 +4207,7 @@ describe('ReactHooksWithNoopRenderer', () => {
42074207
await act(async () => {
42084208
await resolveText('A');
42094209
});
4210-
assertLog([
4211-
'Promise resolved [A]',
4212-
'A',
4213-
'Suspend! [B]',
4214-
4215-
...(gate('enableSiblingPrerendering') ? ['A', 'Suspend! [B]'] : []),
4216-
]);
4210+
assertLog(['Promise resolved [A]', 'A', 'Suspend! [B]']);
42174211

42184212
await act(() => {
42194213
root.render(null);

packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,7 @@ describe('ReactLazy', () => {
198198

199199
await resolveFakeImport(Foo);
200200

201-
await waitForAll([
202-
'Foo',
203-
204-
...(gate('enableSiblingPrerendering') ? ['Foo'] : []),
205-
]);
201+
await waitForAll(['Foo']);
206202
expect(root).not.toMatchRenderedOutput('FooBar');
207203

208204
await act(() => resolveFakeImport(Bar));

0 commit comments

Comments
 (0)