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
Using createRootRoute, Scripts, and HeadContent components adds same script multiple times #4585
When using the Scripts component and defining scripts array inside createRootRoute, the same script gets added multiple times in the head tag. This also results in double scripts for hot reload inside the body
Removing the <Scripts /> component removes the entire client side functionality
Open the devtools using the button to the right of the URL bar (in codesandbox preview)
Open the head tag
Expected behavior
The script tag should be added only once, because it is defined only once
Screenshots or Videos
Platform
Router / Start Version: 1.125.4
OS: Linux (Codesandbox)
Browser: Edge
Browser Version: 139.0.3405.13
Bundler: Vite
Bundler Version: 6.3.5
Additional context
I think this may be caused due to the script being added automatically by createRootRoute, HeadContent and Scripts. There must be a way to filter out duplicate scripts in order to fix this
Reacted by Jelf, Lance Jeffers, Florian Ludot, Daniel Reinoso, Kaan Ozdokmeci, Akhil Vengassery, afsals-xbit, Frej Fredholm, Simen Wiik, Fedir Ushakov and 4 more
changed the title [-]Using Scripts component and createRootRoute adds same script twice[/-][+]Using Scripts component and createRootRoute adds same script multiple times[/+]on Jul 7, 2025
changed the title [-]Using Scripts component and createRootRoute adds same script multiple times[/-][+]Using `createRootRoute`, `Scripts`, and `HeadContent` components adds same script multiple times[/+]on Jul 7, 2025
+1, maybe some added info here is that it doesn't come from tanstack start in my opinion as it is easily replicable with just the basic example of router/basic-file-based which isn't using start.
Just replace export const Route = createFileRoute("/")({ ... }) by
exportconstRoute=createFileRoute("/")({component: Home,scripts: ()=>{return[{id: "homeFaq",type: "application/ld+json",children: JSON.stringify({"@context": "https://schema.org","@type": "FAQPage",mainEntity: [{"@type": "Question",name: "What is the best way to learn React?",acceptedAnswer: {"@type": "Answer",text: "The best way to learn React is to start with the basics and work your way up.",},},],}),},];},});
and you will see the duplication of <script id="homeFaq" type="application/ld+json" /> in the inspector.
@florianldt yeah and this scripts will be added to the <body> tag (through <Scripts /> component). If scripts is inside head it will be put in the <head> tag (through <HeadContent /> component)
As others have suggested, putting the script tag in the head directly works as a temporary workaround.
If you can't use the head tag for some reason, adding the script to a child component with async={true} also seems to be an effective solution due to React's special rendering behavior for <script >
Activity
[-]Using Scripts component and createRootRoute adds same script twice[/-][+]Using Scripts component and createRootRoute adds same script multiple times[/+][-]Using Scripts component and createRootRoute adds same script multiple times[/-][+]Using `createRootRoute`, `Scripts`, and `HeadContent` components adds same script multiple times[/+]max-programming commentedon Jul 7, 2025
Okay, so the problem is more complex than I thought.
razfriman commentedon Jul 10, 2025
Im seeing this too, it is breaking my Google SEO JSON LD support.
https://search.google.com/test/rich-results/result/r%2Freview-snippet?id=Qqz7QsPkCrUBLvXl2m9r2Q
max-programming commentedon Jul 10, 2025
@razfriman for the time being I recommend manually putting the script tag inside the head tag. Anywhere below or above the HeadContent component
florianldt commentedon Jul 12, 2025
+1, maybe some added info here is that it doesn't come from tanstack start in my opinion as it is easily replicable with just the basic example of router/basic-file-based which isn't using start.
Just replace
export const Route = createFileRoute("/")({ ... })byand you will see the duplication of <script id="homeFaq" type="application/ld+json" /> in the inspector.
max-programming commentedon Jul 13, 2025
@florianldt yeah and this
scriptswill be added to the<body>tag (through<Scripts />component). Ifscriptsis insideheadit will be put in the<head>tag (through<HeadContent />component)mbauchet commentedon Jul 31, 2025
Same issue here, Any updates on this?
razfriman commentedon Jul 31, 2025
I was able to replicate it in: https://github.com/razfriman/tanstack-head-dupe
Also managed to replicate it with a singular ROOT route: https://github.com/razfriman/tanstack-head-dupe/blob/root-route/src/routes/__root.tsx
Discord thread: https://discord.com/channels/719702312431386674/1393343640524292166/1393413635807772745
ianjmacintosh commentedon Aug 13, 2025
Ran into this issue today, thanks for documenting!
abielzulio commentedon Aug 29, 2025
facing the same issues, any update?
bskimball commentedon Aug 29, 2025
I just manually insert the script tags as a work around for now
https://github.com/bskimball/tanstack-hono/blob/master/src/routes/__root.tsx
lotap commentedon Sep 4, 2025
I also ran into this issue.
As others have suggested, putting the script tag in the head directly works as a temporary workaround.
If you can't use the head tag for some reason, adding the script to a child component with
async={true}also seems to be an effective solution due to React's special rendering behavior for <script >