Skip to content

Bug: hydration claims the importMap script when <head> renders an inline <script> #38394

Backlog

Description

@mattcosta7

React version: 19.3.1, and 19.3.0-canary-6ff25743-20261117

Steps to reproduce

// app.jsx
export function App() {
  return (
    <html lang="en">
      <head>
        <script id="data" type="application/json">{'{"ok":true}'}</script>
      </head>
      <body>
        <p>hello</p>
      </body>
    </html>
  )
}

// client.jsx
import {hydrateRoot} from 'react-dom/client'
import {App} from './app.jsx'

hydrateRoot(document, <App />, {onRecoverableError: error => console.error(error)})

// server
const stream = await renderToReadableStream(<App />, {
  importMap: {imports: {unused: '/unused.js'}},
  bootstrapModules: ['/client.js'],
})

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.

Activity

  1. 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
  2. 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
  3. mattcosta7 commented on Nov 27, 2026

    @mattcosta7
    ContributorAuthor

    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.

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

    Options

    1. Type check only. This is [Fizz] Don't hydrate head scripts against the importMap script #38395 as it stands.
    2. 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.
    3. 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)
    const importMapScriptStart = stringToPrecomputedChunk(
      '<script type="importmap" data-react-importmap="">',
    );
    
    // ReactFiberConfigDOM.js, `script` case of canHydrateInstance
    if (
      (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?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions