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 mismatch on <script defer> rendered inside <head> #36169
Any app that contains a <script defer>, will cause hydration errors. It is "not recommended" in React, but it either shouldn't cause errors or the docs should explicitly say to not use it.
Error: Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used:
- A server/client branch `if (typeof window !== 'undefined')`.
- Variable input such as `Date.now()` or `Math.random()` which changes each time it's called.
- Date formatting in a user's locale which doesn't match the server.
- External changing data without sending a snapshot of it along with the HTML.
- Invalid HTML tag nesting.
It can also happen if the client has a browser extension installed which messes with the HTML before React loaded.
https://react.dev/link/hydration-mismatch
<App>
<html>
<head>
<script src="app.js" defer={true}>
-
...
The expected behavior
Hydrates with no warnings
Reacted by LoydDev, Adrian Guillen and 5ffhb27r6p-max
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
@cprecioso Could you double check if you can reproduce this in a browser or with jsdom? I wasn't able to reproduce this with jsdom so this might be a happy-dom bug.
Then it seems my reproduction was not minimal enough 😅. I think this is the same issue I'm seeing in my project, but it's only present when I use defer scripts (and not only async) because of an interaction between some tags being reordered by React or Vite and some tags not.
I would still consider this bug, because with defer or without it, the reproduction is using React facilities to do stuff the blessed way (bootstrapModules) and still getting a hydration mismatch.
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!
This still happens on 19.3.1 and on 19.3.0-canary-6ff25743-20261117. As @eps1lon found, defer isn't needed. The trigger is the importMap server option plus any inline <script> that React renders in <head>.
In Chromium, the production build reports minified error #418. React then 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.
Fizz writes the import map into <head> outside the React tree. During hydration, canHydrateInstance walks the <head> children and returns the first <script> node for a script fiber. It only skips async scripts with a src. So the first inline script React renders in <head> claims the import map node.
We work around it with a patch-package patch that adds a guard to the script case of canHydrateInstance:
case "script":
+ if (+ "importmap" === instance.getAttribute("type") &&+ "importmap" !== anyProps.type+ )+ break;
name = instance.getAttribute("src");
The guard has to go in every build that contains the client hydration code: cjs/react-dom-client.development.js, cjs/react-dom-client.production.js, cjs/react-dom-profiling.development.js and cjs/react-dom-profiling.profiling.js. With it, the repro hydrates without errors in both builds, on 19.3.1 and on the canary above. The "importmap" !== anyProps.type check keeps an import map that the app renders itself hydrating against its own node.
Before the patch, we avoided the error without changing React. We rendered <script type="importmap" suppressHydrationWarning> as the first element in <head> on the client only, returning null during server rendering, so the server's import map node had a matching fiber. Keeping inline scripts out of <head> also avoids it.
#37067 proposed the same check and was closed as stale.
Activity
finessor06 commentedon Mar 31, 2026
lotfimaria331-ops commentedon Mar 31, 2026
lotfimaria331-ops commentedon Apr 1, 2026
github-actions commentedon Jun 30, 2026
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
cprecioso commentedon Jul 1, 2026
Still affected
eps1lon commentedon Jul 1, 2026
@cprecioso Could you double check if you can reproduce this in a browser or with
jsdom? I wasn't able to reproduce this withjsdomso this might be ahappy-dombug.cprecioso commentedon Jul 2, 2026
I tweaked the example a bit and can still reproduce in browser and JSDOM:
https://gist.github.com/cprecioso/e639bc150f5c22a7078d3878ddebce22
eps1lon commentedon Jul 2, 2026
I'm getting this mismatch even without
deferthough:cprecioso commentedon Jul 3, 2026
Then it seems my reproduction was not minimal enough 😅. I think this is the same issue I'm seeing in my project, but it's only present when I use
deferscripts (and not onlyasync) because of an interaction between some tags being reordered by React or Vite and some tags not.I would still consider this bug, because with
deferor without it, the reproduction is using React facilities to do stuff the blessed way (bootstrapModules) and still getting a hydration mismatch.Aakriti7-giti commentedon Aug 15, 2026
I would like to work on this issue . I can reproduce it and submit a PR
github-actions commentedon Nov 13, 2026
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!
github-actions commentedon Nov 20, 2026
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!
mattcosta7 commentedon Nov 25, 2026
This still happens on 19.3.1 and on
19.3.0-canary-6ff25743-20261117. As @eps1lon found,deferisn't needed. The trigger is theimportMapserver option plus any inline<script>that React renders in<head>.Minimal repro with React only:
In Chromium, the production build reports minified error #418. React then renders the document on the client, and
<head>ends up with two#datascripts. The development build reports "Hydration failed because the server rendered text didn't match the client", and the diff compares the#datascript with thetype="importmap"node.Fizz writes the import map into
<head>outside the React tree. During hydration,canHydrateInstancewalks the<head>children and returns the first<script>node for a script fiber. It only skips async scripts with asrc. So the first inline script React renders in<head>claims the import map node.We work around it with a patch-package patch that adds a guard to the
scriptcase ofcanHydrateInstance:The guard has to go in every build that contains the client hydration code:
cjs/react-dom-client.development.js,cjs/react-dom-client.production.js,cjs/react-dom-profiling.development.jsandcjs/react-dom-profiling.profiling.js. With it, the repro hydrates without errors in both builds, on 19.3.1 and on the canary above. The"importmap" !== anyProps.typecheck keeps an import map that the app renders itself hydrating against its own node.Before the patch, we avoided the error without changing React. We rendered
<script type="importmap" suppressHydrationWarning>as the first element in<head>on the client only, returningnullduring server rendering, so the server's import map node had a matching fiber. Keeping inline scripts out of<head>also avoids it.#37067 proposed the same check and was closed as stale.