Skip to content

Using createRootRoute, Scripts, and HeadContent components adds same script multiple times #4585

Closed

Description

@max-programming

Which 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

https://codesandbox.io/p/devbox/github/tanstack/router/tree/main/examples/react/start-basic?embed=1&theme=dark&file=src/routes/__root.tsx

Steps to Reproduce the Bug or Issue

  1. Go to the codesandbox url
  2. Open the devtools using the button to the right of the URL bar (in codesandbox preview)
  3. Open the head tag

Expected behavior

The script tag should be added only once, because it is defined only once

Screenshots or Videos

Image Image Image

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

Activity

  1. 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
  2. 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
  3. max-programming commented on Jul 7, 2025

    @max-programming
    ContributorAuthor

    Okay, so the problem is more complex than I thought.

    1. If the page with scripts is directly loaded, 2 script tags get added, and both execute
    2. If the page was navigated to, 2 script tags get added, and only one executes
  4. razfriman commented on Jul 10, 2025

    @razfriman

    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

    Image
  5. max-programming commented on Jul 10, 2025

    @max-programming
    ContributorAuthor

    @razfriman for the time being I recommend manually putting the script tag inside the head tag. Anywhere below or above the HeadContent component

  6. florianldt commented on Jul 12, 2025

    @florianldt

    +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

    export 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 <script id="homeFaq" type="application/ld+json" /> in the inspector.

  7. max-programming commented on Jul 13, 2025

    @max-programming
    ContributorAuthor

    @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)

  8. mbauchet commented on Jul 31, 2025

    @mbauchet

    Same issue here, Any updates on this?

  9. razfriman commented on Jul 31, 2025

    @razfriman
  10. ianjmacintosh commented on Aug 13, 2025

    @ianjmacintosh
    Contributor

    Ran into this issue today, thanks for documenting!

  11. abielzulio commented on Aug 29, 2025

    @abielzulio

    facing the same issues, any update?

  12. bskimball commented on Aug 29, 2025

    @bskimball

    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

  13. lotap commented on Sep 4, 2025

    @lotap

    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 >

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