Skip to content

Fix hydration mismatch for <script> in <head> when using importMap - #37067

Closed
BIGSUS24 wants to merge 1 commit into
react:mainfrom
BIGSUS24:fix/hydration-importmap-script-head
Closed

BIGSUS24 wants to merge 1 commit into
react:mainfrom
BIGSUS24:fix/hydration-importmap-script-head

Conversation

@BIGSUS24

Copy link
Copy Markdown

Fixes #36169.

When prerenderToNodeStream is called with the importMap option, React writes its own <script type="importmap"> directly into <head>. On hydration, canHydrateInstance's script-matching logic in ReactFiberConfigDOM.js walks 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 a src (the typical 3rd-party analytics tag pattern). The injected importmap script has no src and isn't async, 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 the meta/link/style cases right above it already recognize their own known resource shapes.

Repro (adapted from the issue, no defer needed to trigger it once there's an importmap script in head):

const { prelude } = await prerenderToNodeStream(<App />, {
  bootstrapModules: ['./client.js'],
  importMap: { imports: { react: '...' } },
});

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.js that prerenders with both importMap and bootstrapModules set 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 full ReactDOMFloat, ReactDOMHydrationDiff, ReactServerRenderingHydration, ReactDOMFizzShellHydration, and ReactDOMLegacyFloat suites (207 tests) to check for regressions in the shared hydration-matching code — all pass.

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
Copilot AI review requested due to automatic review settings July 19, 2026 19:52
@meta-cla

meta-cla Bot commented Jul 19, 2026

Copy link
Copy Markdown

Hi @BIGSUS24!

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!

Copilot AI 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.

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 importMap and bootstrapModules, 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.

Comment on lines +469 to +480
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);
},
});
});
@meta-cla meta-cla Bot added the CLA Signed label Jul 20, 2026
@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the Resolution: Stale Automatically closed due to inactivity label Oct 21, 2026
@github-actions

Copy link
Copy Markdown

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Resolution: Stale Automatically closed due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Hydration mismatch on <script defer> rendered inside <head>

2 participants