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

Asset.tsx

139 lines3.4 KB

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