Skip to content

Preserve resolved dehydrated Suspense content on context change#623

Open
everettbu wants to merge 1 commit into
mainfrom
fix-22692
Open

Preserve resolved dehydrated Suspense content on context change#623
everettbu wants to merge 1 commit into
mainfrom
fix-22692

Conversation

@everettbu

Copy link
Copy Markdown

Mirror of facebook/react#35914
Original author: schiller-manuel


Summary

When a resolved dehydrated Suspense boundary contains a suspended child, a parent context change can mark the boundary as needing work during hydration. If selective hydration retries are exhausted, React would give up and replace the dehydrated subtree with a client-rendered boundary, destroying the server HTML and showing the fallback.

Preserve the dehydrated fragment in this case when the boundary is resolved and the only change is context (not props).

Fixes react/react#22692

How did you test this change?

Added regression tests

When a resolved dehydrated Suspense boundary contains a suspended child,
a parent context change can mark the boundary as needing work during
hydration. If selective hydration retries are exhausted, React would
give up and replace the dehydrated subtree with a client-rendered
boundary, destroying the server HTML and showing the fallback.

Preserve the dehydrated fragment in this case when the boundary is
resolved and the only change is context (not props).

Fixes react/react#22692
@greptile-apps

greptile-apps Bot commented Feb 26, 2026

Copy link
Copy Markdown

Greptile Summary

Fixes a bug where a context change during hydration could destroy server-rendered HTML in a resolved dehydrated Suspense boundary.

  • When selective hydration retries are exhausted and only context (not props) has changed, the dehydrated fragment is now preserved instead of being replaced with a client-rendered fallback. The fix adds an early return in updateDehydratedSuspenseComponent using the same DidCapture | Callback pattern already used for pending suspense instances.
  • Three regression tests are added/updated covering the scenario described in facebook/react#22692, including context propagation through React.memo boundaries and context consumers inside the Suspense boundary.

Confidence Score: 5/5

  • This PR is safe to merge — it is a targeted, well-scoped fix with comprehensive regression tests.
  • The code change is minimal (13 lines) and follows an existing, well-established pattern in the same function. The three guard conditions (!didReceiveUpdate, !isSuspenseInstancePending, !isSuspenseInstanceFallback) correctly narrow the fix to only the specific scenario described in the issue. The test coverage is thorough, exercising both the happy path and the hydration mismatch case.
  • No files require special attention.

Important Files Changed

Filename Overview
packages/react-reconciler/src/ReactFiberBeginWork.js Adds an early return in updateDehydratedSuspenseComponent when selective hydration retries are exhausted but the boundary is resolved and only context changed. Preserves the dehydrated fragment using the same `DidCapture
packages/react-dom/src/tests/ReactDOMServerPartialHydration-test.internal.js Updates an existing test to expect server HTML preservation instead of fallback, and adds two new regression tests covering context changes through memo boundaries (with and without context consumers inside the Suspense boundary). Tests are thorough and well-structured.

Last reviewed commit: 01bc2d2

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

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.

[React 18] Selective Hydration fails hydration when using context api

2 participants