Repository navigation
Preserve painted SSR fallback DOM when hydration suspends again on a recoverable boundary - #37966
9xj89gzrtw-hue wants to merge 2 commits into
Conversation
…recoverable boundary When hydrating a Suspense boundary whose server-rendered output was a recoverable fallback (e.g. the component requested a browser-only dependency through ReactDOM.browser() and Fizz bailed out), React used to tear down the fallback fiber that hydration began and instead hand the raw DehydratedFragment sibling to beginWork, crashing the work loop with 'Unknown unit of work tag (18)'. More importantly, hydration could re-enter the boundary while the boundary's client data was still pending a second time. In that flow the already-painted fallback DOM instance was deleted and an identical client-rendered fallback was mounted in its place, restarting CSS-driven spinner/progress animations (issue react#37620). This change keeps the server-painted fallback mounted: the retry pass no longer queues its deletion, and the fallback fiber (a DehydratedFragment) is reattached as-is wherever it would previously have been cloned or returned to the work loop. The primary children render into a hidden offscreen fragment so they can still complete when the pending client data resolves, at which point the boundary swaps to content and removes the preserved fallback DOM as usual. A test covers the full flow: SSR paints the fallback, hydration suspends on a browser-only dependency and then again on a client promise, the fallback DOM node keeps its identity throughout, and the content swaps in once the promise resolves.
|
Hi @9xj89gzrtw-hue! 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 f11faf1 |
… test - Marker attribute on the server-painted fallback proves DOM state survives re-suspension on a client promise. - New case: resolving one client promise while the boundary suspends again on a second keeps the same fallback node through the intermediate state.
|
Strengthened the test coverage in the follow-up commit (f11faf1):
Full |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thanks for adding the sequential-suspension coverage. An AI-assisted JSDOM test on The preserved fallback may also need its event handlers connected. |
Preserve painted SSR fallback DOM when hydration suspends again on a recoverable boundary
Fixes #37620
Root cause
When hydration begins on a Suspense boundary whose server-rendered output was a recoverable fallback (e.g. the component requested a browser-only dependency via
ReactDOM.browser()and Fizz bailed out on that boundary), and the primary children suspend again on the client (a client-onlyuse(promise)that is still pending at hydration time), React can re-enter the boundary while its client data is unresolved. The flow is:retrySuspenseComponentWithoutHydratingruns on the restart: it queues the server-paintedDehydratedFragmentfallback fiber for deletion, setsmemoizedState = null, and begins rendering fresh fallback DOM viamountSuspenseFallbackAfterRetryWithoutHydrating.updateDehydratedSuspenseComponentwithdidSuspend = trueand entersmountSuspenseFallbackAfterRetryWithoutHydrating, which deletes the already-painted (SSR) fallback DOM and mounts an identical client-rendered fallback in its place, and makes the new fallback the tracked fallback.Deleting and remounting an identical fallback restarts CSS-driven spinner/progress animations (the reported bug). Worse, depending on ordering, the raw
DehydratedFragment(unit-of-work tag 18) can be handed back to the work loop — e.g. returned fromupdateSuspenseFallbackChildren's tailbailoutOffscreenComponent(null, primaryChildFragment), whosereturnchain ends onprimaryChildFragment.sibling, i.e. the tag-18 fragment — crashing beginWork withUnknown unit of work tag (18).The existing behavior has no way to preserve the fallback fiber because the retry path unconditionally recreates fallback DOM from props.
Fix (3 small hunks, all in
ReactFiberBeginWork.js)When the boundary is being restarted on a recoverable fallback (
isSuspenseInstanceFallback(suspenseInstance) && getSuspenseInstanceFallbackErrorDetails(suspenseInstance).digest === REACT_RECOVERABLE_DIGEST), keep the server-painted fallback mounted instead of deleting it:updateDehydratedSuspenseComponent(restart path): un-queue the deletion of theDehydratedFragment, mount the primary children into a hidden offscreen fragment (mountWorkInProgressOffscreenFiberwithmode: 'hidden') so they can still complete when the pending client data resolves, re-attach the preserved fragment as the boundary's live fallback child, set the standardSUSPENDED_MARKER, and create theOffscreenInstance(the normal path would do this inbailoutOffscreenComponent; here we returnnullso the raw fragment is never begun).updateSuspenseFallbackChildren: when the current fallback child is the preservedDehydratedFragment(tag 18), reattach it as-is instead ofcreateWorkInProgress-cloning it into a position React would try to begin (which crashes on tag 18), and undo the primary pass's queued deletion, clearing the leftoverChildDeletionflag when the deletion list becomes empty.updateSuspenseComponent(showFallback tail): if the fallback sibling on the freshly derived primary child is the preservedDehydratedFragment, returnnullinstead ofbailoutOffscreenComponent(current.child, primaryChildFragment), which would otherwise surface that same tag-18 fragment as the next unit of work.Everything else behaves normally: the retry scheduling that flips a suspended boundary back to content is untouched, so once the client promise resolves the boundary renders its primary content and the preserved fallback DOM is removed in the ordinary way.
Evidence
Base (unpatched) behavior for the new test: the DOM-identity assertion fails with the classic "serializes to the same string" result — the fallback DOM is torn down and an identical node is mounted (and the subsequent resolve flow cannot complete: the first retry render's act Times out) — i.e. the remount bug directly.
After this change, the new test
keeps the fallback DOM when hydration suspends again on a client promisecovers the complete flow:<span>Loading</span>; the boundary bails out on the server (onBrowserBailout).browser()check on the client, then suspends again on a pending client promise; the fallback DOM node keeps its identity (container.querySelector('span')is the same node it was before hydration) and no recoverable errors are dispatched.<span>Content</span>), which is additionally verified after the standard fallback-commit throttle (jest.advanceTimersByTime(FALLBACK_THROTTLE_MS)), matching how other suites in this repo handle throttled retry commits.Also worth noting: the sibling test
can opt a component into browser-only rendering, which was crashing in competing implementations of this issue, passes along with the rest.Tests
packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js: 185/185 passed (including the new test).ReactDOMServerPartialHydration-test.internal.js: 66/67 — the single failure is the pre-existing[GATED, SHOULD FAIL]case (identical on unpatchedmain).ReactSuspense-test.internal,ReactSuspenseFallback-test,ReactSuspenseList-test,ReactSuspenseWithNoopRenderer-test,ReactSuspenseEffectsSemantics-test,ReactSuspenseCallback-test,ReactCPUSuspense-test,ActivitySuspense-test,ActivityLegacySuspense-test): all pass.yarn flow dom-node: no errors.