Skip to content

[Fizz] Don't hydrate head scripts against the importMap script - #38395

Open
mattcosta7 wants to merge 1 commit into
react:mainfrom
mattcosta7:fizz-importmap-hydration
Open

mattcosta7 wants to merge 1 commit into
react:mainfrom
mattcosta7:fizz-importmap-hydration

Conversation

@mattcosta7

@mattcosta7 mattcosta7 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #38394.

renderToReadableStream and renderToPipeableStream write the importMap option into <head> outside the React tree. During hydration, the script case in canHydrateInstance returns the first <script> element unless it is an async script with a src. So the first inline script an app renders in <head> claims the import map node, and hydration fails with error #418. React then renders the document on the client and duplicates the app's head scripts.

This skips type="importmap" elements unless the fiber is itself an import map, so an import map the app renders still hydrates against its own node. #37067 proposed the same check and was closed as stale. #36169 traced the same mismatch.

How did you test this change?

I added two tests to ReactDOMFizzServer-test.js:

  • A document rendered with the importMap option and an inline JSON <script> in <head> hydrates without recoverable errors. The import map node stays in place, and there is still exactly one #data script.
  • An import map the app renders itself still hydrates against its own node.

I couldn't run yarn in my environment, so I haven't run these tests locally. All of this PR's CI checks pass, including every yarn test shard. I formatted the changes with Prettier 3.8 using this repo's options and the Flow parser. With those settings, Prettier leaves the untouched files unchanged.

I verified the same check by applying it to the published react-dom 19.3.1 and 19.3.0-canary-6ff25743-20261117 builds: cjs/react-dom-client.{development,production}.js and cjs/react-dom-profiling.{development,profiling}.js. I then ran the repro from #38394 (server render with importMap, client bundled with esbuild, Chromium):

react-dom production development
unpatched error #418, #data script duplicated "Hydration failed because the server rendered text didn't match the client"
with this check no errors no errors

An import map rendered by the app also hydrated without errors with the check applied.

The `importMap` server option writes a <script type="importmap"> into the
head outside the React tree. canHydrateInstance returned the first script
element for a script fiber unless it was an async script with a src, so
the first inline script an app rendered in <head> claimed the import map
node and hydration failed. Skip import map elements unless the fiber is
itself an import map.

Fixes react#38394

Co-authored-by: Copilot App <[email protected]>
@meta-cla meta-cla Bot added the CLA Signed label Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Comparing: edae846...0b83511

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 7.35 kB 7.35 kB +0.05% 1.96 kB 1.96 kB
oss-stable/react-dom/cjs/react-dom-client.production.js +0.02% 628.58 kB 628.72 kB +0.02% 110.98 kB 111.01 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 7.36 kB 7.36 kB +0.10% 1.96 kB 1.97 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js +0.02% 699.69 kB 699.84 kB +0.02% 122.41 kB 122.43 kB
facebook-www/ReactDOM-prod.classic.js +0.02% 717.77 kB 717.92 kB +0.02% 125.46 kB 125.49 kB
facebook-www/ReactDOM-prod.modern.js +0.02% 708.11 kB 708.26 kB +0.02% 123.88 kB 123.91 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by sizebot against 0b83511

// if we learn it is problematic
if (
element.getAttribute('type') === 'importmap' &&
anyProps.type !== 'importmap'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could we add a test where both the server option and the app render an import map? It looks like hydration could still match the wrong script since both have type="importmap".

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: hydration claims the importMap script when <head> renders an inline <script>

2 participants