()
| 5 | import type { AnyRouteMatch, RouterManagedTag } from '@tanstack/router-core' |
| 6 | |
| 7 | export const Scripts = () => { |
| 8 | const router = useRouter() |
| 9 | const nonce = router.options.ssr?.nonce |
| 10 | |
| 11 | const getAssetScripts = (matches: Array<AnyRouteMatch>) => { |
| 12 | const assetScripts: Array<RouterManagedTag> = [] |
| 13 | const manifest = router.ssr?.manifest |
| 14 | |
| 15 | if (!manifest) { |
| 16 | return [] |
| 17 | } |
| 18 | |
| 19 | for (const match of matches) { |
| 20 | const scripts = manifest.routes[match.routeId]?.scripts |
| 21 | |
| 22 | if (!scripts) { |
| 23 | continue |
| 24 | } |
| 25 | |
| 26 | for (const asset of scripts) { |
| 27 | assetScripts.push({ |
| 28 | tag: 'script', |
| 29 | attrs: { ...asset.attrs, nonce }, |
| 30 | children: asset.children, |
| 31 | }) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return assetScripts |
| 36 | } |
| 37 | |
| 38 | const getScripts = (matches: Array<AnyRouteMatch>): Array<RouterManagedTag> => |
| 39 | ( |
| 40 | matches |
| 41 | .map((match) => match.scripts!) |
| 42 | .flat(1) |
| 43 | .filter(Boolean) as Array<RouterManagedTag> |
| 44 | ).map( |
| 45 | ({ children, ...script }) => |
| 46 | ({ |
| 47 | tag: 'script', |
| 48 | attrs: { |
| 49 | ...script, |
| 50 | nonce, |
| 51 | }, |
| 52 | children, |
| 53 | }) satisfies RouterManagedTag, |
| 54 | ) |
| 55 | |
| 56 | const activeMatches = Solid.createMemo(() => router.stores.matches.get()) |
| 57 | const assetScripts = Solid.createMemo(() => getAssetScripts(activeMatches())) |
| 58 | const scripts = Solid.createMemo(() => getScripts(activeMatches())) |
| 59 | |
| 60 | return renderScripts(router, scripts(), assetScripts()) |
| 61 | } |
| 62 | |
| 63 | function renderScripts( |
| 64 | router: ReturnType<typeof useRouter>, |
nothing calls this directly
no test coverage detected