Skip to content

Preserve painted SSR fallback DOM when hydration suspends again on a recoverable boundary - #37966

Open
9xj89gzrtw-hue wants to merge 2 commits into
react:mainfrom
9xj89gzrtw-hue:fix/preserve-recoverable-fallback-during-hydration
Open

9xj89gzrtw-hue wants to merge 2 commits into
react:mainfrom
9xj89gzrtw-hue:fix/preserve-recoverable-fallback-during-hydration

Conversation

@9xj89gzrtw-hue

Copy link
Copy Markdown

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-only use(promise) that is still pending at hydration time), React can re-enter the boundary while its client data is unresolved. The flow is:

  1. Pass 1 begins the dehydrated boundary and attempts the primary children. Because they suspend, retrySuspenseComponentWithoutHydrating runs on the restart: it queues the server-painted DehydratedFragment fallback fiber for deletion, sets memoizedState = null, and begins rendering fresh fallback DOM via mountSuspenseFallbackAfterRetryWithoutHydrating.
  2. Pass 2 lands in updateDehydratedSuspenseComponent with didSuspend = true and enters mountSuspenseFallbackAfterRetryWithoutHydrating, 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 from updateSuspenseFallbackChildren's tail bailoutOffscreenComponent(null, primaryChildFragment), whose return chain ends on primaryChildFragment.sibling, i.e. the tag-18 fragment — crashing beginWork with Unknown 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:

  1. updateDehydratedSuspenseComponent (restart path): un-queue the deletion of the DehydratedFragment, mount the primary children into a hidden offscreen fragment (mountWorkInProgressOffscreenFiber with mode: '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 standard SUSPENDED_MARKER, and create the OffscreenInstance (the normal path would do this in bailoutOffscreenComponent; here we return null so the raw fragment is never begun).
  2. updateSuspenseFallbackChildren: when the current fallback child is the preserved DehydratedFragment (tag 18), reattach it as-is instead of createWorkInProgress-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 leftover ChildDeletion flag when the deletion list becomes empty.
  3. updateSuspenseComponent (showFallback tail): if the fallback sibling on the freshly derived primary child is the preserved DehydratedFragment, return null instead of bailoutOffscreenComponent(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 promise covers the complete flow:

  • SSR paints <span>Loading</span>; the boundary bails out on the server (onBrowserBailout).
  • Hydration passes the 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.
  • Resolving the client promise swaps in the content (<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 unpatched main).
  • Reconciler suites (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.

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

meta-cla Bot commented Oct 22, 2026

Copy link
Copy Markdown

Hi @9xj89gzrtw-hue!

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 22, 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 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.
@9xj89gzrtw-hue

Copy link
Copy Markdown
Author

Strengthened the test coverage in the follow-up commit (f11faf1):

  1. The existing re-suspension test now sets a data-kept attribute on the server-painted fallback and verifies the marker survives the second suspension — proof that the same node (not an identical re-mount) stays in the DOM.
  2. New case keeps the fallback DOM across sequential client suspensions: resolving one client promise while the boundary suspends again on a second keeps the same fallback node through the intermediate state, then the content swaps in once both resolve.

Full ReactDOMFizzServer-test.js suite: 186/186 passing locally.

@meta-cla

meta-cla Bot commented Oct 23, 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!

@meta-cla meta-cla Bot added the CLA Signed label Oct 23, 2026
@meta-cla

meta-cla Bot commented Oct 23, 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!

@catlover-bot

Copy link
Copy Markdown

Thanks for adding the sequential-suspension coverage. An AI-assisted JSDOM test on f11faf1 found that, in the use(browser()) case, clicking the visible fallback button after the hydration attempt while the primary client promise remains pending does not call onClick (expected: 1, actual: 0). The same test passes on base 9ffcb463, which replaces the server fallback.

The preserved fallback may also need its event handlers connected.

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)

3 participants