Repository navigation
[Fizz] Don't hydrate head scripts against the importMap script - #38395
Open
mattcosta7 wants to merge 1 commit into
Open
mattcosta7 wants to merge 1 commit into
mattcosta7 wants to merge 1 commit into
Conversation
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]>
|
Comparing: edae846...0b83511 Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: (No significant changes) Generated by sizebot against 0b83511 |
mattcosta7
marked this pull request as ready for review
November 25, 2026 20:01
ietuday
reviewed
Nov 27, 2026
| // if we learn it is problematic | ||
| if ( | ||
| element.getAttribute('type') === 'importmap' && | ||
| anyProps.type !== 'importmap' |
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #38394.
renderToReadableStreamandrenderToPipeableStreamwrite theimportMapoption into<head>outside the React tree. During hydration, thescriptcase incanHydrateInstancereturns the first<script>element unless it is an async script with asrc. 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:importMapoption and an inline JSON<script>in<head>hydrates without recoverable errors. The import map node stays in place, and there is still exactly one#datascript.I couldn't run
yarnin my environment, so I haven't run these tests locally. All of this PR's CI checks pass, including everyyarn testshard. 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-dom19.3.1 and19.3.0-canary-6ff25743-20261117builds:cjs/react-dom-client.{development,production}.jsandcjs/react-dom-profiling.{development,profiling}.js. I then ran the repro from #38394 (server render withimportMap, client bundled with esbuild, Chromium):#datascript duplicatedAn import map rendered by the app also hydrated without errors with the check applied.