Skip to content

Preserve painted SSR fallback DOM when hydration re-suspends on recoverable boundary - #38026

Open
MilindChatrabhuji wants to merge 1 commit into
react:mainfrom
MilindChatrabhuji:fix/suspense-fallback-hydration-remount-37620
Open

MilindChatrabhuji wants to merge 1 commit into
react:mainfrom
MilindChatrabhuji:fix/suspense-fallback-hydration-remount-37620

Conversation

@MilindChatrabhuji

@MilindChatrabhuji MilindChatrabhuji commented Oct 28, 2026 •

Copy link
Copy Markdown

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 with REACT_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 setTimeout promise, localStorage, or IndexedDB query):

  1. In the first pass of hydration retry (retrySuspenseComponentWithoutHydrating), React queues the server-painted DehydratedFragment fallback fiber for deletion, clears memoizedState, and begins rendering primary children.
  2. When the primary children suspend on the client promise, React re-enters the boundary in the second pass (updateDehydratedSuspenseComponent with didSuspend = true).
  3. Previously, React fell through to 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 bailoutOffscreenComponent can hand the raw DehydratedFragment (unit of work tag 18) directly back to the work loop, crashing with Unknown unit of work tag (18).

Fix

When restarting on a recoverable fallback (isSuspenseInstanceFallback(suspenseInstance) && getSuspenseInstanceFallbackErrorDetails(suspenseInstance).digest === REACT_RECOVERABLE_DIGEST):

  1. updateDehydratedSuspenseComponent (restart path): Un-queue the deletion of DehydratedFragment, mount the primary children into a hidden Offscreen fiber (mode: 'hidden'), re-attach the preserved dehydrated fragment as the boundary's fallback sibling, set SUSPENDED_MARKER, initialize the OffscreenInstance, and return null so the raw fragment is never begun by the work loop.
  2. updateSuspenseFallbackChildren: When current fallback child is the preserved DehydratedFragment, reattach it as-is without cloning, and undo the primary pass's queued deletion (clearing ChildDeletion if empty).
  3. updateSuspenseComponent (showFallback tail): If primaryChildFragment.sibling is the preserved DehydratedFragment, return null instead of calling bailoutOffscreenComponent, 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 pass
    • keeps 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 suspensions
    • can opt a component into browser-only rendering after streaming the fallback
  • Suspense reconciler suites all pass (100%):
    • ReactSuspenseFallback-test.js: PASS
    • ReactSuspense-test.internal.js: PASS
    • ReactSuspenseList-test.js: PASS
    • ReactSuspenseCallback-test.js: PASS
    • ReactCPUSuspense-test.js: PASS
    • ActivityLegacySuspense-test.js: PASS
    • ActivitySuspense-test.js: PASS
    • ReactSuspenseEffectsSemantics-test.js: PASS
    • ReactSuspenseWithNoopRenderer-test.js: PASS
    • ReactDOMServerPartialHydration-test.internal.js: PASS (67/67)

…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.
@meta-cla

meta-cla Bot commented Oct 28, 2026

Copy link
Copy Markdown

Hi @MilindChatrabhuji!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

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

@meta-cla

meta-cla Bot commented Oct 28, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Suspense fallback remounts on hydration after use(browser()) + client use(promise)

1 participant