Open History to see the last commit
1import { _getAssetMatches } from '../load-client'
2import type { Manifest, RouterManagedTag } from '../manifest'
3import type { AnyRouteMatch } from '../Matches'
4import type { InitialHydrationScriptTags } from './hydrationScripts'
5β
6type ScriptTags = Array<RouterManagedTag>
7export type SsrBodyScriptParts = readonly [ScriptTags, ScriptTags]
8β
9export function getSsrBodyScriptParts(
10 matches: Array<AnyRouteMatch>,
11 manifest: Manifest | undefined,
12 nonce: string | undefined,
13 routeScriptAttrs?: Record<string, unknown>,
14): SsrBodyScriptParts {
15 const assetMatches = _getAssetMatches(matches)
16 const routeScripts: ScriptTags = []
17 const manifestScripts: ScriptTags = []
18 for (const match of assetMatches) {
19 for (const script of Array.isArray(match.scripts) ? match.scripts : []) {
20 if (!script) {
21 continue
22 }
23 const { children, ...attrs } = script
24 routeScripts.push({
25 tag: 'script',
26 attrs: { ...attrs, ...routeScriptAttrs, nonce },
27 children,
28 })
29 }
30 }
31 if (manifest) {
32 for (const match of assetMatches) {
33 for (const asset of manifest.routes[match.routeId]?.scripts ?? []) {
34 manifestScripts.push({
35 tag: 'script',
36 attrs: { ...asset.attrs, nonce },
37 children: asset.children,
38 })
39 }
40 }
41 }
42 return [routeScripts, manifestScripts]
43}
44export function composeSsrBodyScripts(
45 [routeScripts, manifestScripts]: SsrBodyScriptParts,
46 initialHydrationScripts?: InitialHydrationScriptTags,
47): Array<RouterManagedTag> {
48 if (!initialHydrationScripts) {
49 return [...routeScripts, ...manifestScripts]
50 }
51 return [
52 ...initialHydrationScripts.before,
53 ...routeScripts,
54 ...manifestScripts,
55 initialHydrationScripts.boundary,
56 ]
57}