Skip to content

[Fiber] Restore the singleton scope when replaying a suspended HostSingleton - #37630

Open
h-zahar wants to merge 1 commit into
react:mainfrom
h-zahar:fix/singleton-scope-replay-hydration
Open

h-zahar wants to merge 1 commit into
react:mainfrom
h-zahar:fix/singleton-scope-replay-hydration

Conversation

@h-zahar

@h-zahar h-zahar commented Sep 14, 2026 •

Copy link
Copy Markdown

Summary

Fixes #37551.

If something inside <head> suspends during hydration, everything after it stops hydrating. The server-rendered nodes in <body> get thrown out and rebuilt on the client.

<head> is a scope - entering stashes the hydration cursor, exiting restores it. When a child suspends, replayBeginWork re-runs beginWork on the same fiber, so <head> gets claimed a second time without having exited, and that claim clobbers the stashed cursor.

replayBeginWork already restores hydration state for HostComponent (#35494). This does the same for HostSingleton.

#35494 does have a HostSingleton test, but its lazy child sits in <body> - a singleton with no scope, so the cursor never gets touched. That's how <head> slipped through.

HostHoistable isn't included, it can't suspend this way. unwindWork still has its // TODO: popHydrationState, left alone.

Different approach: a three-line fix that just skips the save when the slot's full. Same tests pass. But went with the restore since it's the reconciler that skipped the exit.

How did you test this change?

Added three tests to ReactDOMServerPartialHydration-test.internal.js next to the #35494 ones - empty <head>, <head> with server-rendered content, and <head> replayed twice. Each checks there's no recoverable error and that document.body.firstChild is still the same node, i.e. it actually hydrated instead of getting replaced.

On main:

● ReactDOMServerPartialHydration › Can hydrate even when lazy content resumes immediately inside a scoped HostSingleton
 
    - Array []
    + Array [
    +   "Hydration failed because the server rendered HTML didn't match the client.",
    + ]
 
      4482 |     expect(hydrationErrors).toEqual([]);

With the fix. Run them one at a time — they hydrate document, so a leftover root
from an earlier test in the file hides the failure:

$ yarn test ReactDOMServerPartialHydration -t "inside a scoped HostSingleton"
Tests: 69 skipped, 1 passed, 70 total
 
$ yarn test ReactDOMServerPartialHydration -t "existing content is replayed"
Tests: 69 skipped, 1 passed, 70 total
 
$ yarn test ReactDOMServerPartialHydration -t "replayed more than once"
Tests: 69 skipped, 1 passed, 70 total

I also ran the repro from the issue as-is against published [email protected].
head is the reported case, body is its control:

stock               head   recoverableErrors: 1   bodyDivReused: false
                    body   recoverableErrors: 0   bodyDivReused: true
 
with the fix        head   recoverableErrors: 0   bodyDivReused: true
                    body   recoverableErrors: 0   bodyDivReused: true

Full sweep at 9b93853278:

$ yarn test packages/react-dom packages/react-reconciler
Test Suites: 225 passed, 225 total
Tests:       21 skipped, 5383 passed, 5404 total

@meta-cla meta-cla Bot added the CLA Signed label Sep 14, 2026
@github-actions

github-actions Bot commented Sep 14, 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 d23f606

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: hydration mismatch when a suspended <head> (HostSingleton) is replayed — singleton-scope cursor is saved twice

1 participant