Skip to content

Commit

Permalink
Register Suspense retry handlers in commit phase
Browse files Browse the repository at this point in the history
To avoid GC pressure and accidentally hanging onto old trees Suspense boundary retries are now implemented in the commit phase. I used the Callback flag which was previously only used to schedule callbacks for Class components. This isn't quite semantically equivalent but it's unused and seemingly compatible.
  • Loading branch information
gnoff committed Dec 4, 2024
1 parent 16d2bbb commit 410219a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
21 changes: 11 additions & 10 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -1310,20 +1310,21 @@ export function registerSuspenseInstanceRetry(
callback: () => void,
) {
const ownerDocument = instance.ownerDocument;
if (ownerDocument.readyState !== DOCUMENT_READY_STATE_COMPLETE) {
ownerDocument.addEventListener(
'DOMContentLoaded',
() => {
if (instance.data === SUSPENSE_PENDING_START_DATA) {
if (ownerDocument.readyState !== DOCUMENT_READY_STATE_COMPLETE) {
const listener = () => {
if (instance.data === SUSPENSE_PENDING_START_DATA) {
callback();
}
},
{
once: true,
},
);
ownerDocument.removeEventListener('DOMContentLoaded', listener);
};
ownerDocument.addEventListener('DOMContentLoaded', listener);
instance._reactRetry = callback;
} else {
// The document entered complete state before the retry could be registered.
callback();
}
}
instance._reactRetry = callback;
}

export function canHydrateFormStateMarker(
Expand Down
9 changes: 3 additions & 6 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import {
PerformedWork,
Placement,
Hydrating,
Callback,
ContentReset,
DidCapture,
Update,
Expand Down Expand Up @@ -166,7 +167,6 @@ import {
isSuspenseInstancePending,
isSuspenseInstanceFallback,
getSuspenseInstanceFallbackErrorDetails,
registerSuspenseInstanceRetry,
supportsHydration,
supportsResources,
supportsSingletons,
Expand Down Expand Up @@ -256,7 +256,6 @@ import {
isFunctionClassComponent,
} from './ReactFiber';
import {
retryDehydratedSuspenseBoundary,
scheduleUpdateOnFiber,
renderDidSuspendDelayIfPossible,
markSkippedUpdateLanes,
Expand Down Expand Up @@ -2816,12 +2815,10 @@ function updateDehydratedSuspenseComponent(
// on the client than if we just leave it alone. If the server times out or errors
// these should update this boundary to the permanent Fallback state instead.
// Mark it as having captured (i.e. suspended).
workInProgress.flags |= DidCapture;
// Also Mark it as requiring retry.
workInProgress.flags |= DidCapture | Callback;
// Leave the child in place. I.e. the dehydrated fragment.
workInProgress.child = current.child;
// Register a callback to retry this boundary once the server has sent the result.
const retry = retryDehydratedSuspenseBoundary.bind(null, current);
registerSuspenseInstanceRetry(suspenseInstance, retry);
return null;
} else {
// This is the first attempt.
Expand Down
19 changes: 19 additions & 0 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ import {
suspendInstance,
suspendResource,
resetFormInstance,
registerSuspenseInstanceRetry,
} from './ReactFiberConfig';
import {
captureCommitPhaseError,
Expand All @@ -154,6 +155,7 @@ import {
addMarkerProgressCallbackToPendingTransition,
addMarkerIncompleteCallbackToPendingTransition,
addMarkerCompleteCallbackToPendingTransition,
retryDehydratedSuspenseBoundary,
} from './ReactFiberWorkLoop';
import {
HasEffect as HookHasEffect,
Expand Down Expand Up @@ -526,6 +528,23 @@ function commitLayoutEffectOnFiber(
if (flags & Update) {
commitSuspenseHydrationCallbacks(finishedRoot, finishedWork);
}
if (flags & Callback) {
// This Boundary is in fallback and has a dehydrated Suspense instance.
// We could in theory assume the dehydrated state but we recheck it for
// certainty.
const finishedState: SuspenseState | null = finishedWork.memoizedState;
if (finishedState !== null) {
const dehydrated = finishedState.dehydrated;
if (dehydrated !== null) {
// Register a callback to retry this boundary once the server has sent the result.
const retry = retryDehydratedSuspenseBoundary.bind(
null,
finishedWork,
);
registerSuspenseInstanceRetry(dehydrated, retry);
}
}
}
break;
}
case OffscreenComponent: {
Expand Down

0 comments on commit 410219a

Please sign in to comment.