Repository navigation
Conversation
When prerenderToNodeStream is called with importMap, React injects its own <script type="importmap"> into <head>. During hydration, canHydrateInstance's script-matching heuristic only knew how to skip past unrelated 3rd-party async scripts, so it would wrongly accept the importmap script as a match for the app's own <script> in <head>, causing a hydration mismatch. Skip the importmap script the same way other known resource scripts are already skipped. Fixes react#36169
|
Hi @BIGSUS24! 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! |
There was a problem hiding this comment.
Pull request overview
Fixes a hydration mismatch when React injects an <script type="importmap"> into <head> (via prerenderToNodeStream({importMap})) and the client hydration algorithm mistakenly matches that import map script against an app-authored <script> in <head>.
Changes:
- Update DOM hydration matching to treat
type="importmap"scripts as a known “skip” shape when scanning<head>for a<script>match. - Add a regression test that prerenders with both
importMapandbootstrapModules, hydrates a document containing a<script>in<head>, and asserts no recoverable hydration errors.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js | Skip type="importmap" scripts during <script> instance matching in singleton/head hydration scanning. |
| packages/react-dom/src/tests/ReactDOMFizzStatic-test.js | Add regression coverage for hydration with an injected import map script plus an app <script> in <head>. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const recoverableErrors = []; | ||
| global.requestAnimationFrame = global.window.requestAnimationFrame = cb => | ||
| setTimeout(cb); | ||
| // Use the real (scheduler-flushing) act so hydration actually traverses. | ||
| const clientAct = require('internal-test-utils').act; | ||
| await clientAct(async () => { | ||
| ReactDOMClient.hydrateRoot(doc, <App />, { | ||
| onRecoverableError(error) { | ||
| recoverableErrors.push(error.message); | ||
| }, | ||
| }); | ||
| }); |
|
This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated. |
|
Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you! |
Fixes #36169.
When
prerenderToNodeStreamis called with theimportMapoption, React writes its own<script type="importmap">directly into<head>. On hydration,canHydrateInstance's script-matching logic inReactFiberConfigDOM.jswalks the head's DOM children looking for a match for each script fiber, and it's supposed to skip past script tags it doesn't recognize as the one it's looking for. The skip condition only covered one shape though: an async script with asrc(the typical 3rd-party analytics tag pattern). The injected importmap script has nosrcand isn'tasync, so it fell through and got accepted as the match for the app's own<script>in<head>, producing a hydration mismatch.This adds a check for
type="importmap"to the existing skip condition, the same way themeta/link/stylecases right above it already recognize their own known resource shapes.Repro (adapted from the issue, no
deferneeded to trigger it once there's an importmap script in head):Verified this hangs together with @eps1lon's finding in the issue thread (script gets matched against
src={null} type="importmap"instead of its own props).Added a regression test to
ReactDOMFizzStatic-test.jsthat prerenders with bothimportMapandbootstrapModulesset and a<script>in<head>, then hydrates and asserts no recoverable errors. Confirmed it fails without the fix and passes with it. Also ran the fullReactDOMFloat,ReactDOMHydrationDiff,ReactServerRenderingHydration,ReactDOMFizzShellHydration, andReactDOMLegacyFloatsuites (207 tests) to check for regressions in the shared hydration-matching code — all pass.