Open History to see the last commit
1import * as Solid from 'solid-js'
2import {
3 _getAssetMatches,
4 appendUniqueUserTags,
5 escapeHtml,
6 getAssetCrossOrigin,
7 getScriptPreloadAttrs,
8 resolveManifestCssLink,
9} from '@tanstack/router-core'
10import { useRouter } from './useRouter'
11import type {
12 AssetCrossOriginConfig,
13 RouterManagedTag,
14} from '@tanstack/router-core'
15β
16/**
17 * Build the head/link/meta/script tags from the renderable presented prefix.
18 * Used internally by `HeadContent`.
19 */
20export const useTags = (assetCrossOrigin?: AssetCrossOriginConfig) => {
21 const router = useRouter()
22 const nonce = router.options.ssr?.nonce
23 return Solid.createMemo((prev: Array<RouterManagedTag> | undefined) => {
24 const matches = _getAssetMatches(router.stores.matches.get())
25 const resultMeta: Array<RouterManagedTag> = []
26 const metaByAttribute: Record<string, true> = {}
27 let title: RouterManagedTag | undefined
28 for (let i = matches.length - 1; i >= 0; i--) {
29 const metas = matches[i]!.meta
30 if (!metas) {
31 continue
32 }
33 for (let j = metas.length - 1; j >= 0; j--) {
34 const m = metas[j]
35 if (!m) {
36 continue
37 }
38β
39 if (m.title) {
40 if (!title) {
41 title = {
42 tag: 'title',
43 children: m.title,
44 }
45 }
46 } else if ('script:ld+json' in m) {
47 // Handle JSON-LD structured data
48 // Content is HTML-escaped to prevent XSS when injected via innerHTML
49 try {
50 const json = JSON.stringify(m['script:ld+json'])
51 resultMeta.push({
52 tag: 'script',
53 attrs: {
54 type: 'application/ld+json',
55 },
56 children: escapeHtml(json),
57 })
58 } catch {
59 // Skip invalid JSON-LD objects
60 }
61 } else {
62 const attribute = m.name ?? m.property
63 if (attribute) {
64 if (metaByAttribute[attribute]) {
65 continue
66 } else {
67 metaByAttribute[attribute] = true
68 }
69 }
70β
71 resultMeta.push({
72 tag: 'meta',
73 attrs: {
74 ...m,
75 nonce,
76 },
77 })
78 }
79 }
80 }
81β
82 if (title) {
83 resultMeta.push(title)
84 }
85β
86 if (router.options.ssr?.nonce) {
87 resultMeta.push({
88 tag: 'meta',
89 attrs: {
90 property: 'csp-nonce',
91 content: router.options.ssr.nonce,
92 },
93 })
94 }
95 resultMeta.reverse()
96β
97 const next: Array<RouterManagedTag> = []
98 appendUniqueUserTags(next, resultMeta)
99 const manifest = router.ssr?.manifest
100 const preloads: Array<RouterManagedTag> = []
101 for (const match of matches) {
102 for (const preload of manifest?.routes[match.routeId]?.preloads ?? []) {
103 if (!preload) {
104 continue
105 }
106 preloads.push({
107 tag: 'link',
108 attrs: {
109 ...getScriptPreloadAttrs(manifest, preload, assetCrossOrigin),
110 nonce,
111 },
112 })
113 }
114 }
115 next.push(...preloads)
116β
117 const links: Array<RouterManagedTag> = []
118 for (const match of matches) {
119 for (const link of match.links ?? []) {
120 if (link === undefined) {
121 continue
122 }
123 links.push({ tag: 'link', attrs: { ...link, nonce } })
124 }
125 }
126 appendUniqueUserTags(next, links)
127β
128 if (manifest) {
129 for (const match of matches) {
130 for (const link of manifest.routes[match.routeId]?.css ?? []) {
131 const resolvedLink = resolveManifestCssLink(link)
132 next.push({
133 tag: 'link',
134 attrs: {
135 rel: 'stylesheet',
136 ...resolvedLink,
137 crossOrigin:
138 getAssetCrossOrigin(assetCrossOrigin, 'stylesheet') ??
139 resolvedLink.crossOrigin,
140 nonce,
141 },
142 })
143 }
144 }
145 if (manifest.inlineStyle) {
146 next.push({
147 tag: 'style',
148 attrs: {
149 ...manifest.inlineStyle.attrs,
150 nonce,
151 },
152 children: manifest.inlineStyle.children,
153 inlineCss: true,
154 })
155 }
156 }
157β
158 const styles: Array<RouterManagedTag> = []
159 const headScripts: Array<RouterManagedTag> = []
160 for (const match of matches) {
161 for (const style of match.styles ?? []) {
162 if (style === undefined) {
163 continue
164 }
165 const { children, ...attrs } = style
166 styles.push({
167 tag: 'style',
168 attrs: { ...attrs, nonce },
169 children: children as string | undefined,
170 })
171 }
172 for (const script of match.headScripts ?? []) {
173 if (script === undefined) {
174 continue
175 }
176 const { children, ...attrs } = script
177 headScripts.push({
178 tag: 'script',
179 attrs: {
180 ...attrs,
181 nonce,
182 },
183 children: children as string | undefined,
184 })
185 }
186 }
187 appendUniqueUserTags(next, styles)
188 appendUniqueUserTags(next, headScripts)
189β
190 if (prev === undefined) {
191 return next
192 }
193 return replaceEqualTags(prev, next)
194 })
195}
196β
197function replaceEqualTags(
198 prev: Array<RouterManagedTag>,
199 next: Array<RouterManagedTag>,
200) {
201 const prevByKey = new Map<string, RouterManagedTag>()
202 for (const tag of prev) {
203 prevByKey.set(JSON.stringify(tag), tag)
204 }
205β
206 let isEqual = prev.length === next.length
207 const result = next.map((tag, index) => {
208 const existing = prevByKey.get(JSON.stringify(tag))
209 if (existing) {
210 if (existing !== prev[index]) {
211 isEqual = false
212 }
213 return existing
214 }
215β
216 isEqual = false
217 return tag
218 })
219β
220 return isEqual ? prev : result
221}