({
attrs,
children,
}: {
attrs?: ScriptAttrs
children?: string
})
| 43 | } |
| 44 | |
| 45 | function Script({ |
| 46 | attrs, |
| 47 | children, |
| 48 | }: { |
| 49 | attrs?: ScriptAttrs |
| 50 | children?: string |
| 51 | }) { |
| 52 | const router = useRouter() |
| 53 | const hydrated = useHydrated() |
| 54 | const dataScript = |
| 55 | typeof attrs?.type === 'string' && |
| 56 | attrs.type !== '' && |
| 57 | attrs.type !== 'text/javascript' && |
| 58 | attrs.type !== 'module' |
| 59 | |
| 60 | if ( |
| 61 | process.env.NODE_ENV !== 'production' && |
| 62 | attrs?.src && |
| 63 | typeof children === 'string' && |
| 64 | children.trim().length |
| 65 | ) { |
| 66 | console.warn( |
| 67 | '[TanStack Router] <Script> received both `src` and `children`. The `children` content will be ignored. Remove `children` or remove `src`.', |
| 68 | ) |
| 69 | } |
| 70 | |
| 71 | React.useEffect(() => { |
| 72 | if (dataScript) return |
| 73 | |
| 74 | if (attrs?.src) { |
| 75 | const normSrc = (() => { |
| 76 | try { |
| 77 | const base = document.baseURI || window.location.href |
| 78 | return new URL(attrs.src, base).href |
| 79 | } catch { |
| 80 | return attrs.src |
| 81 | } |
| 82 | })() |
| 83 | const existingScript = Array.from( |
| 84 | document.querySelectorAll('script[src]'), |
| 85 | ).find((el) => (el as HTMLScriptElement).src === normSrc) |
| 86 | |
| 87 | if (existingScript) { |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | const script = document.createElement('script') |
| 92 | |
| 93 | for (const [key, value] of Object.entries(attrs)) { |
| 94 | if ( |
| 95 | key !== 'suppressHydrationWarning' && |
| 96 | value !== undefined && |
| 97 | value !== false |
| 98 | ) { |
| 99 | script.setAttribute( |
| 100 | key, |
| 101 | typeof value === 'boolean' ? '' : String(value), |
| 102 | ) |
nothing calls this directly
no test coverage detected