Using `createRootRoute`, `Scripts`, and `HeadContent` components adds same script multiple times #4585
Send to my agentWhich project does this relate to?#
Start
Describe the bug#
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
Your Example Website or App#
Steps to Reproduce the Bug or Issue#
- Go to the codesandbox url
- 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
max-programming(GitHub profile, opens in a new tab) changed the title
max-programming(GitHub profile, opens in a new tab) changed the title
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
@razfriman for the time being I recommend manually putting the script tag inside the head tag. Anywhere below or above the HeadContent component
+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("/")({ ... })byexport const Route = 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 [removed] in the inspector.
@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)Same issue here, Any updates on this?
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
Ran into this issue today, thanks for documenting!
facing the same issues, any update?
facing the same issues, any update?
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
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 [removed]
Sign in to join this conversation.
Okay, so the problem is more complex than I thought.