Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from facebook:main #22

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -1310,20 +1310,28 @@ 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) {
callback();
}
},
{
once: true,
},
);
if (
// The Fizz runtime must have put this boundary into client render or complete
// state after the render finished but before it committed. We need to call the
// callback now rather than wait
instance.data !== SUSPENSE_PENDING_START_DATA ||
// The boundary is still in pending status but the document has finished loading
// before we could register the event handler that would have scheduled the retry
// on load so we call teh callback now.
ownerDocument.readyState === DOCUMENT_READY_STATE_COMPLETE
) {
callback();
} else {
// We're still in pending status and the document is still loading so we attach
// a listener to the document load even and expose the retry on the instance for
// the Fizz runtime to trigger if it ends up resolving this boundary
const listener = () => {
callback();
ownerDocument.removeEventListener('DOMContentLoaded', listener);
};
ownerDocument.addEventListener('DOMContentLoaded', listener);
instance._reactRetry = listener;
}
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
Loading