Skip to content

[Fiber] Propagate context into a dehydrated boundary on an early bailout - #38399

Open
LouisCuvelier wants to merge 1 commit into
react:mainfrom
LouisCuvelier:fix-dehydrated-context-propagation
Open

LouisCuvelier wants to merge 1 commit into
react:mainfrom
LouisCuvelier:fix-dehydrated-context-propagation

Conversation

@LouisCuvelier

Copy link
Copy Markdown

Summary

Fixes #38398.

A dehydrated Suspense or Activity boundary with no work of its own goes through attemptEarlyBailoutIfNoScheduledUpdate, which returns null as soon as the boundary is dehydrated, before any lazy context propagation. The parent does not always propagate for it. When another child already has work in the render lanes, bailoutOnAlreadyFinishedWork clones the children without propagating. When the parent renders itself, for example a provider whose value changes while its children stay the same, it does not propagate either. In both cases the boundary never learns that a parent context changed. The update commits with the boundary still dehydrated, and when React hydrates it later, it reads the new context value against the server HTML and throws a hydration mismatch (#418 in production).

This change runs lazilyPropagateParentContextChanges in the dehydrated branches of attemptEarlyBailoutIfNoScheduledUpdate, for both SuspenseComponent and ActivityComponent. When a context changed, it takes the normal path (updateSuspenseComponent / updateActivityComponent). That path already handles a context change on a dehydrated boundary: it schedules a hydration at a higher priority, so the boundary hydrates with the old value before the update applies. The timed-out Suspense branch just below already propagates the same way.

We hit this in a Next.js App Router app. A layout renders a parallel-route slot in a <Suspense> next to client components that update right after hydration. A reload followed by Back within a second made the slot hydrate the previous page's HTML with the new page's tree.

How did you test this change?

Six new tests, three in ReactDOMServerPartialHydration-test.internal.js for Suspense and three in ReactDOMServerPartialHydrationActivity-test.internal.js for Activity:

  • A context changes and, in the same lane, a sibling of the dehydrated boundary updates.
  • A context changes in the parent that renders the boundary, with no sibling work.
  • The first case, with the boundary still suspended during hydration. Suspense shows its fallback, and Activity shows the parent's fallback, with no error.

The first two cases assert that the boundary hydrates with the old value, then takes the update, keeps its DOM node, and reports no recoverable error.

Without the fix in ReactFiberBeginWork.js, all six fail. The sibling case on Suspense, for example:

- Expected
+ Received

  Array [
-   "Child: Hello",
+   "Sibling updated",
    "Child: Hi",
-   "Sibling updated",
+   "Child: Hi",
+   "onRecoverableError: Hydration failed because the server rendered text didn't match the client.",
  ]

With the fix, all six pass, and the 116 tests of the two files pass.

Commands run (Node 26):

yarn test packages/react-dom packages/react-reconciler in six configurations, on the final commit:

Configuration Passed Failed Skipped
default (experimental) 5212 3 21
-r=stable 5212 2 21
-r=www-modern --variant=true 5149 1 86
-r=www-modern --variant=false 5210 5 21
--prod 5204 1 21
-r=stable --prod 5202 3 21

None of the failures comes from this change:

  • ReactDOMFizzShellHydration › recovers from a large component stack during SSR fails in every configuration with ReferenceError: __DEV__ is not defined. It fails on main too, and looks tied to running Node 26 instead of the repo's Node 24.
  • ReactDOMFizzServer › can recover from very deep trees… (stable, prod) fails on main too. One of the two deep-tree tests fails on each run.
  • ReactMultiChildText, ReactUpdates and ReactDOMServerSelectiveHydration › regression: selective hydration does not contribute to "maximum update limit" count pass when rerun alone with this change, in the configuration where they failed. They failed while the machine ran the full suite under load.

Also run:

  • yarn flow dom-node: no errors.
  • prettier --check and eslint on the changed files: clean.

In our app, the same change applied to the React that Next.js bundles removed the error on the Back-after-reload repro (0 errors in 6 runs, against 3 in 5 without it).

🤖 Generated with Claude Code

A dehydrated Suspense or Activity boundary that has no work of its own
takes the early bailout, which returns before the lazy context
propagation. The parent does not always propagate for it: it skips the
propagation when it rendered itself, or when another child already has
work in the render lanes. The boundary then never learns that a parent
context changed. The update commits with the boundary still dehydrated,
and the boundary later hydrates the server HTML with the new context
value, which throws a hydration mismatch.

Propagate parent context changes in that branch, as the timed-out
Suspense branch already does. When a context changed, take the normal
path, which hydrates the boundary at a higher priority before applying
the update.

Fixes react#38398

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@meta-cla

meta-cla Bot commented Nov 26, 2026

Copy link
Copy Markdown

Hi @LouisCuvelier!

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

Copy link
Copy Markdown

A size report will appear here when the build finishes.

Generated by sizebot against 7648a42

@meta-cla

meta-cla Bot commented Nov 26, 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 Nov 26, 2026

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: a dehydrated Suspense boundary misses a context change when a sibling has work in the same lanes, then hydrates with the new value (#418)

1 participant