Skip to content
Source
Sign inSign up
TanStack/router

The content/children script tags of the head (`scripts[].children`) are lost from the HTML after hydration #7104

Openrenomureza(GitHub profile, opens in a new tab) opened this issue 2 comments

Which project does this relate to?

Router

Describe the bug

export const Route = createFileRoute('/test')({
  component: RouteComponent,
  head: () => ({
    scripts: [
      {
        async: true,
        src: `https://www.googletagmanager.com/gtag/js?id=ID`,
      },
      {
        // this is missing on hydrated HTML
        children: `window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'ID');`,
      },
    ],
  }),
});

Children code in scripts (<script>window.dataLayer...</script>) disappears after hydration on first page visit, reappears if navigating first to another page.

Your Example Website or App

https://stackblitz.com/edit/github-zpekad3w-lxs8xvjn?file=src%2Froutes%2Ftest%2Froute.tsx

Steps to Reproduce the Bug or Issue

  1. Build the attached StackBlitz site (npm run build)
  2. node .output/server/index.mjs
  3. navigate to the test page
  4. Refresh the page and check the HTML element in the console <script>window.dataLayer...</script> is missing.

Expected behavior

<script>window.dataLayer...</script> should not disappear after hydration

Screenshots or Videos

https://github.com/user-attachments/assets/badce2ce-7cb9-4ab0-a749-844ecb48fa8c

Platform

  • Router / Start Version: 1.168.10 / 1.167.16
  • OS: Windows (WSL 2)
  • Browser: Chrome
  • Browser Version: 146.0.7680.178
  • Bundler: vite
  • Bundler Version: 8.0.3

Additional context

No response

  1. Expected behavior

    <script>window.dataLayer...</script> should not disappear after hydration

    why does this matter? window.dataLayer has the state

  2. schiller-manuel(GitHub profile, opens in a new tab) added labelsinformation needed

  3. I think the content is executed, so window.dataLayer has state. However, users will likely assume something is wrong because it disappears when debugging via the browser's inspect element on the first visit; this isn't common behavior.

Sign in to join this conversation.