MCPcopy Create free account
hub / github.com/TanStack/router / Script

Function Script

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

Source from the content-addressed store, hash-verified

63}
64
65function Script({
66 attrs,
67 children,
68}: {
69 attrs?: ScriptAttrs
70 children?: string
71}): JSX.Element | null {
72 const router = useRouter()
73 const dataScript =
74 typeof attrs?.type === 'string' &&
75 attrs.type !== '' &&
76 attrs.type !== 'text/javascript' &&
77 attrs.type !== 'module'
78
79 onMount(() => {
80 if (dataScript) return
81
82 if (attrs?.src) {
83 const normSrc = (() => {
84 try {
85 const base = document.baseURI || window.location.href
86 return new URL(attrs.src, base).href
87 } catch {
88 return attrs.src
89 }
90 })()
91 const existingScript = Array.from(
92 document.querySelectorAll('script[src]'),
93 ).find((el) => (el as HTMLScriptElement).src === normSrc)
94
95 if (existingScript) {
96 return
97 }
98
99 const script = document.createElement('script')
100
101 for (const [key, value] of Object.entries(attrs)) {
102 if (value !== undefined && value !== false) {
103 script.setAttribute(
104 key,
105 typeof value === 'boolean' ? '' : String(value),
106 )
107 }
108 }
109
110 document.head.appendChild(script)
111
112 onCleanup(() => {
113 if (script.parentNode) {
114 script.parentNode.removeChild(script)
115 }
116 })
117 }
118
119 if (typeof children === 'string') {
120 const typeAttr =
121 typeof attrs?.type === 'string' ? attrs.type : 'text/javascript'
122 const nonceAttr =

Callers

nothing calls this directly

Calls 3

useRouterFunction · 0.90
setAttributeMethod · 0.65
getAttributeMethod · 0.65

Tested by

no test coverage detected