Repository navigation
[Fiber] Propagate context into a dehydrated boundary on an early bailout - #38399
LouisCuvelier wants to merge 1 commit into
Conversation
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]>
|
Hi @LouisCuvelier! 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! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Summary
Fixes #38398.
A dehydrated Suspense or Activity boundary with no work of its own goes through
attemptEarlyBailoutIfNoScheduledUpdate, which returnsnullas 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,bailoutOnAlreadyFinishedWorkclones 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
lazilyPropagateParentContextChangesin the dehydrated branches ofattemptEarlyBailoutIfNoScheduledUpdate, for bothSuspenseComponentandActivityComponent. 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.jsfor Suspense and three inReactDOMServerPartialHydrationActivity-test.internal.jsfor Activity: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: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-reconcilerin six configurations, on the final commit:-r=stable-r=www-modern --variant=true-r=www-modern --variant=false--prod-r=stable --prodNone of the failures comes from this change:
ReactDOMFizzShellHydration › recovers from a large component stack during SSRfails in every configuration withReferenceError: __DEV__ is not defined. It fails onmaintoo, and looks tied to running Node 26 instead of the repo's Node 24.ReactDOMFizzServer › can recover from very deep trees…(stable, prod) fails onmaintoo. One of the two deep-tree tests fails on each run.ReactMultiChildText,ReactUpdatesandReactDOMServerSelectiveHydration › regression: selective hydration does not contribute to "maximum update limit" countpass 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 --checkandeslinton 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