Repository navigation
Preserve painted SSR fallback DOM when hydration re-suspends on recoverable boundary - #38026
MilindChatrabhuji wants to merge 1 commit into
Conversation
…ds on recoverable boundary Fixes react#37620 When a Suspense boundary outputs a recoverable server fallback (e.g. via use(browser())) and the primary children suspend again on the client during hydration (such as on a client promise created via setTimeout or browser storage), React previously deleted the server-rendered fallback and mounted a fresh client fallback, restarting CSS animations and resetting DOM state. This fix preserves the server-painted fallback DOM: 1. In updateDehydratedSuspenseComponent: when restarting a recoverable fallback boundary that re-suspends, un-queue the DehydratedFragment deletion, mount the primary children into a hidden Offscreen fiber, reattach the preserved fragment as fallback sibling, set SUSPENDED_MARKER, and return null so the raw DehydratedFragment is never begun. 2. In updateSuspenseFallbackChildren: when current fallback child is a DehydratedFragment, reattach it as-is without cloning and un-queue any deletion. 3. In updateSuspenseComponent: when primaryChildFragment.sibling is a DehydratedFragment, return null instead of calling bailoutOffscreenComponent, preventing tag 18 from being surfaced as a unit of work. Regression tests added to ReactDOMFizzServer-test verifying fallback DOM identity and state preservation across client re-suspends.
|
Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
The build for this commit did not complete, so there is no size report. See the workflow run for details. Generated by sizebot against 619b412 |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Preserve painted SSR fallback DOM when hydration re-suspends on recoverable boundary
Fixes #37620
Root Cause
When a Suspense boundary outputs a recoverable fallback on the server (e.g. when a component requests browser-only rendering via
ReactDOM.browser()and Fizz bails out on that boundary withREACT_RECOVERABLE_DIGEST), the server fallback DOM is painted and visible to the user.If the component then suspends again on the client (e.g. on a client-only promise such as a
setTimeoutpromise,localStorage, or IndexedDB query):retrySuspenseComponentWithoutHydrating), React queues the server-paintedDehydratedFragmentfallback fiber for deletion, clearsmemoizedState, and begins rendering primary children.updateDehydratedSuspenseComponentwithdidSuspend = true).mountSuspenseFallbackAfterRetryWithoutHydrating, which deleted the already-painted server fallback and mounted a fresh client-rendered fallback.Deleting and remounting an identical fallback resets DOM state and restarts CSS-driven spinner/progress animations (the reported issue in #37620). Furthermore, in some update orderings, returning from
bailoutOffscreenComponentcan hand the rawDehydratedFragment(unit of work tag 18) directly back to the work loop, crashing withUnknown unit of work tag (18).Fix
When restarting on a recoverable fallback (
isSuspenseInstanceFallback(suspenseInstance) && getSuspenseInstanceFallbackErrorDetails(suspenseInstance).digest === REACT_RECOVERABLE_DIGEST):updateDehydratedSuspenseComponent(restart path): Un-queue the deletion ofDehydratedFragment, mount the primary children into a hidden Offscreen fiber (mode: 'hidden'), re-attach the preserved dehydrated fragment as the boundary's fallback sibling, setSUSPENDED_MARKER, initialize theOffscreenInstance, and returnnullso the raw fragment is never begun by the work loop.updateSuspenseFallbackChildren: When current fallback child is the preservedDehydratedFragment, reattach it as-is without cloning, and undo the primary pass's queued deletion (clearingChildDeletionif empty).updateSuspenseComponent(showFallback tail): IfprimaryChildFragment.siblingis the preservedDehydratedFragment, returnnullinstead of callingbailoutOffscreenComponent, preventing tag 18 from being surfaced as a unit of work.When the client promise resolves, standard Suspense retry scheduling swaps the primary content in and tears down the preserved fallback DOM normally.
Test Plan
ReactDOMFizzServer-test: regression tests passkeeps the fallback DOM when hydration suspends again on a client promise(asserts fallback DOM identity and state retention)keeps the fallback DOM across sequential client suspensionscan opt a component into browser-only rendering after streaming the fallbackReactSuspenseFallback-test.js: PASSReactSuspense-test.internal.js: PASSReactSuspenseList-test.js: PASSReactSuspenseCallback-test.js: PASSReactCPUSuspense-test.js: PASSActivityLegacySuspense-test.js: PASSActivitySuspense-test.js: PASSReactSuspenseEffectsSemantics-test.js: PASSReactSuspenseWithNoopRenderer-test.js: PASSReactDOMServerPartialHydration-test.internal.js: PASS (67/67)