MCPcopy
hub / github.com/TanStack/router / Script

Function Script

packages/react-router/src/Asset.tsx:45–219  ·  view source on GitHub ↗
({
  attrs,
  children,
}: {
  attrs?: ScriptAttrs
  children?: string
})

Source from the content-addressed store, hash-verified

43}
44
45function 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 )

Callers

nothing calls this directly

Calls 6

useRouterFunction · 0.90
useHydratedFunction · 0.90
trimMethod · 0.80
warnMethod · 0.65
setAttributeMethod · 0.65
getAttributeMethod · 0.65

Tested by

no test coverage detected