You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: hydration claims the importMap script when <head> renders an inline <script> #38394
Serve the stream, load the page, and let the client hydrate.
Current behavior
The production build reports minified error #418. React renders the document on the client, and <head> ends up with two #data scripts. The development build reports "Hydration failed because the server rendered text didn't match the client", and the diff compares the #data script with the type="importmap" node.
Expected behavior
Hydration succeeds and leaves the import map node alone.
Cause
Fizz writes the importMap option into <head> outside the React tree. In canHydrateInstance, the script case returns the first <script> element unless it is an async script with a src. The import map has neither, so the first inline script React renders in <head> claims it.
Skipping type="importmap" elements unless the fiber is itself an import map fixes it. With that check, the repro hydrates without errors in production and development builds.
Related: #36169, which was closed as stale and whose last comments trace the same mismatch, and #37067, which proposed the same check and was closed as stale.
changed the title [-]Bug: hydration claims the `importMap` script when `<head>` renders an inline `<script>`[/-][+]Bug: import map hydration mismatches with inline `<head>` scripts and `preloadModule`[/+]on Nov 26, 2026
changed the title [-]Bug: import map hydration mismatches with inline `<head>` scripts and `preloadModule`[/-][+]Bug: hydration claims the `importMap` script when `<head>` renders an inline `<script>`[/+]on Nov 26, 2026
I looked into the case @ietuday raised on #38395, where an app renders its own <script type="importmap"> and also passes the importMap option. #38395 doesn't fix that case. I prototyped three ways to handle it and would like a maintainer's read on which one to take.
Scripts that aren't import maps skip import maps during hydration. That fixes the repro in this issue and every other head script shape I tried: inline, JSON, dangerouslySetInnerHTML, src, and module scripts, with and without nonce, and in a non-document root. It also fixes a case that is silent in production. A head <script dangerouslySetInnerHTML>, such as a theme script, hydrates against the import map without an error, and unmounting it later deletes the import map.
What's left
Fizz writes the option's import map ahead of the app's head children, so an import map the app renders still claims the option's node. With children that fails with #418. With dangerouslySetInnerHTML it binds silently in production, and unmounting it deletes the option's import map.
Chrome 133+ and Safari 18.4+ support multiple import maps (caniuse), so this is a valid setup. The comment in writeHoistables says React doesn't expect apps to render import maps as tags. Merging the app's map into the importMap option avoids the problem today.
Rank import maps by content on the client. Among import maps, prefer the one whose text would hydrate for the fiber, using the same checkForUnmatchedText comparison as hydrateProperties, and fall back to the first import map if none matches. The fallback is required. Without it, a dangerouslySetInnerHTML import map whose content differs between server and client, which only warns in development today, fails hydration and the whole document renders on the client.
Mark the option's script on the server. Fizz adds an attribute to the option's import map, and import map fibers skip scripts that have it. canHydrateInstance already skips stylesheet resources this way using data-precedence.
The marker prototype is small. The attribute name is a placeholder:
// ReactFizzConfigDOM.js (and the nonce variant)constimportMapScriptStart=stringToPrecomputedChunk('<script type="importmap" data-react-importmap="">',);// ReactFiberConfigDOM.js, `script` case of canHydrateInstanceif((element.getAttribute('type')==='importmap')!==(anyProps.type==='importmap')){break;}if(anyProps.type==='importmap'&&element.hasAttribute('data-react-importmap')){break;}
Results
I ran 18 hydration tests: each head script shape next to the option's import map, multiple import maps, nonce, CRLF content, an import map with the same content as the option, a non-document root, and import maps whose content differs from the server. Each test unmounts the app's scripts afterward to check which node every fiber owns. Every result was the same in development and production builds. The tests are on a local branch, and I can add them to #38395.
main
1. Type check
2. Content ranking
3. Marker
18 hydration tests
14 fail
5 fail
pass
pass
App import map whose content differs between server and client, plus the option
fails
fails
fails
passes
Changes server HTML
no
no
yes, 3 output tests change
Reads script text in canHydrateInstance
no
yes
no
The two approaches have different costs:
Content ranking needs no server change and works with HTML from any server version. It adds a text comparison to a function whose script heuristics avoid one today, and it can't tell the import maps apart when the app's content differs between server and client.
The marker changes the HTML of every page that uses the importMap option. With HTML from an older server that has no marker, the client behaves like option 1. I tested that: the same 5 tests fail and the rest pass. Import maps that other tools inject aren't marked.
Recommendation
I'd land #38395 with the type check, since it fixes this issue and the silent production case on its own. Then I'd handle multiple import maps in a follow-up with the marker, because it's deterministic, reads no content, and follows the data-precedence pattern. If changing the server output isn't acceptable, content ranking with the fallback is the alternative.
Would you take the marker, and do you have a preference for the attribute name?
Activity
[-]Bug: hydration claims the `importMap` script when `<head>` renders an inline `<script>`[/-][+]Bug: import map hydration mismatches with inline `<head>` scripts and `preloadModule`[/+][-]Bug: import map hydration mismatches with inline `<head>` scripts and `preloadModule`[/-][+]Bug: hydration claims the `importMap` script when `<head>` renders an inline `<script>`[/+]mattcosta7 commentedon Nov 27, 2026
I looked into the case @ietuday raised on #38395, where an app renders its own
<script type="importmap">and also passes theimportMapoption. #38395 doesn't fix that case. I prototyped three ways to handle it and would like a maintainer's read on which one to take.What #38395 fixes
Scripts that aren't import maps skip import maps during hydration. That fixes the repro in this issue and every other head script shape I tried: inline, JSON,
dangerouslySetInnerHTML,src, and module scripts, with and withoutnonce, and in a non-document root. It also fixes a case that is silent in production. A head<script dangerouslySetInnerHTML>, such as a theme script, hydrates against the import map without an error, and unmounting it later deletes the import map.What's left
Fizz writes the option's import map ahead of the app's head children, so an import map the app renders still claims the option's node. With
childrenthat fails with #418. WithdangerouslySetInnerHTMLit binds silently in production, and unmounting it deletes the option's import map.Chrome 133+ and Safari 18.4+ support multiple import maps (caniuse), so this is a valid setup. The comment in
writeHoistablessays React doesn't expect apps to render import maps as tags. Merging the app's map into theimportMapoption avoids the problem today.Options
checkForUnmatchedTextcomparison ashydrateProperties, and fall back to the first import map if none matches. The fallback is required. Without it, adangerouslySetInnerHTMLimport map whose content differs between server and client, which only warns in development today, fails hydration and the whole document renders on the client.canHydrateInstancealready skips stylesheet resources this way usingdata-precedence.The marker prototype is small. The attribute name is a placeholder:
Results
I ran 18 hydration tests: each head script shape next to the option's import map, multiple import maps,
nonce, CRLF content, an import map with the same content as the option, a non-document root, and import maps whose content differs from the server. Each test unmounts the app's scripts afterward to check which node every fiber owns. Every result was the same in development and production builds. The tests are on a local branch, and I can add them to #38395.canHydrateInstanceThe two approaches have different costs:
importMapoption. With HTML from an older server that has no marker, the client behaves like option 1. I tested that: the same 5 tests fail and the rest pass. Import maps that other tools inject aren't marked.Recommendation
I'd land #38395 with the type check, since it fixes this issue and the silent production case on its own. Then I'd handle multiple import maps in a follow-up with the marker, because it's deterministic, reads no content, and follows the
data-precedencepattern. If changing the server output isn't acceptable, content ranking with the fallback is the alternative.Would you take the marker, and do you have a preference for the attribute name?