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

Asset.tsx

165 lines3.8 KB

1import * as React from 'react'
2import { useRouter } from './useRouter'
3import type { RouterManagedTag } from '@tanstack/router-core'
4​
5interface ScriptAttrs {
6 [key: string]: string | boolean | undefined
7 src?: string
8 suppressHydrationWarning?: boolean
9}
10​
11export function Asset({
12 tag,
13 attrs,
14 children,
15 nonce,
16}: RouterManagedTag & { nonce?: string }): React.ReactElement | null {
17 switch (tag) {
18 case 'title':
19 return (
20 <title {...attrs} suppressHydrationWarning>
21 {children}
22 </title>
23 )
24 case 'meta':
25 return <meta {...attrs} suppressHydrationWarning />
26 case 'link':
27 return <link {...attrs} nonce={nonce} suppressHydrationWarning />
28 case 'style':
29 return (
30 <style
31 {...attrs}
32 dangerouslySetInnerHTML={{ __html: children as string }}
33 nonce={nonce}
34 />
35 )
36 case 'script':
37 return <Script attrs={attrs}>{children}</Script>
38 default:
39 return null
40 }
42​
43function Script({
44 attrs,
45 children,
46}: {
47 attrs?: ScriptAttrs
48 children?: string
49}) {
50 const router = useRouter()
51​
52 React.useEffect(() => {
53 if (attrs?.src) {
54 const normSrc = (() => {
55 try {
56 const base = document.baseURI || window.location.href
57 return new URL(attrs.src, base).href
58 } catch {
59 return attrs.src
60 }
61 })()
62 const existingScript = Array.from(
63 document.querySelectorAll('script[src]'),
64 ).find((el) => (el as HTMLScriptElement).src === normSrc)
65​
66 if (existingScript) {
67 return
68 }
69​
70 const script = document.createElement('script')
71​
72 for (const [key, value] of Object.entries(attrs)) {
73 if (
74 key !== 'suppressHydrationWarning' &&
75 value !== undefined &&
76 value !== false
77 ) {
78 script.setAttribute(
79 key,
80 typeof value === 'boolean' ? '' : String(value),
81 )
82 }
83 }
84​
85 document.head.appendChild(script)
86​
87 return () => {
88 if (script.parentNode) {
89 script.parentNode.removeChild(script)
90 }
91 }
92 }
93​
94 if (typeof children === 'string') {
95 const typeAttr =
96 typeof attrs?.type === 'string' ? attrs.type : 'text/javascript'
97 const nonceAttr =
98 typeof attrs?.nonce === 'string' ? attrs.nonce : undefined
99 const existingScript = Array.from(
100 document.querySelectorAll('script:not([src])'),
101 ).find((el) => {
102 if (!(el instanceof HTMLScriptElement)) return false
103 const sType = el.getAttribute('type') ?? 'text/javascript'
104 const sNonce = el.getAttribute('nonce') ?? undefined
105 return (
106 el.textContent === children &&
107 sType === typeAttr &&
108 sNonce === nonceAttr
109 )
110 })
111​
112 if (existingScript) {
113 return
114 }
115​
116 const script = document.createElement('script')
117 script.textContent = children
118​
119 if (attrs) {
120 for (const [key, value] of Object.entries(attrs)) {
121 if (
122 key !== 'suppressHydrationWarning' &&
123 value !== undefined &&
124 value !== false
125 ) {
126 script.setAttribute(
127 key,
128 typeof value === 'boolean' ? '' : String(value),
129 )
130 }
131 }
132 }
133​
134 document.head.appendChild(script)
135​
136 return () => {
137 if (script.parentNode) {
138 script.parentNode.removeChild(script)
139 }
140 }
141 }
142​
143 return undefined
144 }, [attrs, children])
145​
146 if (!router.isServer) {
147 return null
148 }
149​
150 if (attrs?.src && typeof attrs.src === 'string') {
151 return <script {...attrs} suppressHydrationWarning />
152 }
153​
154 if (typeof children === 'string') {
155 return (
156 <script
157 {...attrs}
158 dangerouslySetInnerHTML={{ __html: children }}
159 suppressHydrationWarning
160 />
161 )
162 }
163​
164 return null