Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

ScriptOnce.tsx

21 lines511 Bytes

1import { isServer } from '@tanstack/router-core/isServer'
2import { useRouter } from './useRouter'
3​
4/**
5 * Server-only helper to emit a script tag exactly once during SSR.
6 */
7export function ScriptOnce({ children }: { children: string }) {
8 const router = useRouter()
9 if (!(isServer ?? router.isServer)) {
10 return null
11 }
12​
13 return (
14 <script
15 nonce={router.options.ssr?.nonce}
16 dangerouslySetInnerHTML={{
17 __html: children + ';document.currentScript.remove()',
18 }}
19 />
20 )