Skip to content
Source
Sign inSign up
TanStack/router

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

Send to my agent
Closedmax-programming(GitHub profile, opens in a new tab) opened this issue 11 comments

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

  1. 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
  2. @razfriman for the time being I recommend manually putting the script tag inside the head tag. Anywhere below or above the HeadContent component

  3. +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 [removed] in the inspector.

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

  5. Same issue here, Any updates on this?

  6. Ran into this issue today, thanks for documenting!

  7. facing the same issues, any update?

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

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