Skip to content

Commit

Permalink
Fix: useDeferredValue initialValue suspends forever without switching…
Browse files Browse the repository at this point in the history
… to final (#27888)

Fixes a bug in the experimental `initialValue` option for
`useDeferredValue` (added in #27500).

If rendering the `initialValue` causes the tree to suspend, React should
skip it and switch to rendering the final value instead. It should not
wait for `initialValue` to resolve.

This is not just an optimization, because in some cases the initial
value may _never_ resolve — intentionally. For example, if the
application does not provide an instant fallback state. This capability
is, in fact, the primary motivation for the `initialValue` API.

I mostly implemented this correctly in the original PR, but I missed
some cases where it wasn't working:

- If there's no Suspense boundary between the `useDeferredValue` hook
and the component that suspends, and we're not in the shell of the
transition (i.e. there's a parent Suspense boundary wrapping the
`useDeferredValue` hook), the deferred task would get incorrectly
dropped.
- Similarly, if there's no Suspense boundary between the
`useDeferredValue` hook and the component that suspends, and we're
rendering a synchronous update, the deferred task would get incorrectly
dropped.

What these cases have in common is that it causes the `useDeferredValue`
hook itself to be replaced by a Suspense fallback. The fix was the same
for both. (It already worked in cases where there's no Suspense fallback
at all, because those are handled differently, at the root.)

The way I discovered this was when investigating a particular bug in
Next.js that would happen during a 'popstate' transition (back/forward),
but not during a regular navigation. That's because we render popstate
transitions synchronously to preserve browser's scroll position — which
in this case triggered the second scenario above.

DiffTrain build for commit f1039be.
  • Loading branch information
acdlite committed Jan 8, 2024
1 parent 89da9fc commit 92eda60
Show file tree
Hide file tree
Showing 13 changed files with 908 additions and 524 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<1664f9f3eec9720b0472e436e7889c65>>
* @generated SignedSource<<b85e6b4f686f35ddcd77f85e17cfc9f9>>
*/

"use strict";
Expand Down Expand Up @@ -488,6 +488,7 @@ if (__DEV__) {

var ScheduleRetry = StoreConsistency;
var ShouldSuspendCommit = Visibility;
var DidDefer = ContentReset;
var LifecycleEffectMask =
Passive$1 | Update | Callback | Ref | Snapshot | StoreConsistency; // Union of all commit flags (flags with the lifetime of a particular commit)

Expand Down Expand Up @@ -13945,9 +13946,26 @@ if (__DEV__) {
return hasSuspenseListContext(suspenseContext, ForceSuspenseFallback);
}

function getRemainingWorkInPrimaryTree(current, renderLanes) {
// TODO: Should not remove render lanes that were pinged during this render
return removeLanes(current.childLanes, renderLanes);
function getRemainingWorkInPrimaryTree(
current,
primaryTreeDidDefer,
renderLanes
) {
var remainingLanes =
current !== null
? removeLanes(current.childLanes, renderLanes)
: NoLanes;

if (primaryTreeDidDefer) {
// A useDeferredValue hook spawned a deferred task inside the primary tree.
// Ensure that we retry this component at the deferred priority.
// TODO: We could make this a per-subtree value instead of a global one.
// Would need to track it on the context stack somehow, similar to what
// we'd have to do for resumable contexts.
remainingLanes = mergeLanes(remainingLanes, peekDeferredLane());
}

return remainingLanes;
}

function updateSuspenseComponent(current, workInProgress, renderLanes) {
Expand All @@ -13967,7 +13985,12 @@ if (__DEV__) {
// rendering the fallback children.
showFallback = true;
workInProgress.flags &= ~DidCapture;
} // OK, the next part is confusing. We're about to reconcile the Suspense
} // Check if the primary children spawned a deferred task (useDeferredValue)
// during the first pass.

var didPrimaryChildrenDefer =
(workInProgress.flags & DidDefer) !== NoFlags$1;
workInProgress.flags &= ~DidDefer; // OK, the next part is confusing. We're about to reconcile the Suspense
// boundary's children. This involves some custom reconciliation logic. Two
// main reasons this is so complicated.
//
Expand Down Expand Up @@ -14005,6 +14028,11 @@ if (__DEV__) {
var primaryChildFragment = workInProgress.child;
primaryChildFragment.memoizedState =
mountSuspenseOffscreenState(renderLanes);
primaryChildFragment.childLanes = getRemainingWorkInPrimaryTree(
current,
didPrimaryChildrenDefer,
renderLanes
);
workInProgress.memoizedState = SUSPENDED_MARKER;

return fallbackFragment;
Expand All @@ -14028,6 +14056,7 @@ if (__DEV__) {
current,
workInProgress,
didSuspend,
didPrimaryChildrenDefer,
nextProps,
_dehydrated,
prevState,
Expand Down Expand Up @@ -14056,6 +14085,7 @@ if (__DEV__) {

_primaryChildFragment2.childLanes = getRemainingWorkInPrimaryTree(
current,
didPrimaryChildrenDefer,
renderLanes
);
workInProgress.memoizedState = SUSPENDED_MARKER;
Expand Down Expand Up @@ -14374,6 +14404,7 @@ if (__DEV__) {
current,
workInProgress,
didSuspend,
didPrimaryChildrenDefer,
nextProps,
suspenseInstance,
suspenseState,
Expand Down Expand Up @@ -14575,6 +14606,11 @@ if (__DEV__) {
var _primaryChildFragment4 = workInProgress.child;
_primaryChildFragment4.memoizedState =
mountSuspenseOffscreenState(renderLanes);
_primaryChildFragment4.childLanes = getRemainingWorkInPrimaryTree(
current,
didPrimaryChildrenDefer,
renderLanes
);
workInProgress.memoizedState = SUSPENDED_MARKER;
return fallbackChildFragment;
}
Expand Down Expand Up @@ -21570,10 +21606,22 @@ if (__DEV__) {
// Everything else is spawned as a transition.
workInProgressDeferredLane = requestTransitionLane();
}
} // Mark the parent Suspense boundary so it knows to spawn the deferred lane.

var suspenseHandler = getSuspenseHandler();

if (suspenseHandler !== null) {
// TODO: As an optimization, we shouldn't entangle the lanes at the root; we
// can entangle them using the baseLanes of the Suspense boundary instead.
// We only need to do something special if there's no Suspense boundary.
suspenseHandler.flags |= DidDefer;
}

return workInProgressDeferredLane;
}
function peekDeferredLane() {
return workInProgressDeferredLane;
}
function scheduleUpdateOnFiber(root, fiber, lane) {
{
if (isRunningInsertionEffect) {
Expand Down Expand Up @@ -22142,7 +22190,7 @@ if (__DEV__) {
// The render unwound without completing the tree. This happens in special
// cases where need to exit the current render without producing a
// consistent tree or committing.
markRootSuspended(root, lanes, NoLane);
markRootSuspended(root, lanes, workInProgressDeferredLane);
ensureRootIsScheduled(root);
return null;
} // We now have a consistent tree. Because this is a sync render, we
Expand Down Expand Up @@ -25476,7 +25524,7 @@ if (__DEV__) {
return root;
}

var ReactVersion = "18.3.0-canary-1d5667a12-20240102";
var ReactVersion = "18.3.0-canary-f1039be4a-20240107";

// Might add PROFILE later.

Expand Down
Loading

0 comments on commit 92eda60

Please sign in to comment.