-
Notifications
You must be signed in to change notification settings - Fork 27.2k
/
get-layer-assets.tsx
96 lines (88 loc) · 2.92 KB
/
get-layer-assets.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React from 'react'
import { getLinkAndScriptTags } from './get-css-inlined-link-tags'
import { getPreloadableFonts } from './get-preloadable-fonts'
import type { AppRenderContext } from './app-render'
import { getAssetQueryString } from './get-asset-query-string'
import { encodeURIPath } from '../../shared/lib/encode-uri-path'
import type { PreloadCallbacks } from './types'
import { renderCssResource } from './render-css-resource'
export function getLayerAssets({
ctx,
layoutOrPagePath,
injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
preloadCallbacks,
}: {
layoutOrPagePath: string | undefined
injectedCSS: Set<string>
injectedJS: Set<string>
injectedFontPreloadTags: Set<string>
ctx: AppRenderContext
preloadCallbacks: PreloadCallbacks
}): React.ReactNode {
const { styles: styleTags, scripts: scriptTags } = layoutOrPagePath
? getLinkAndScriptTags(
ctx.clientReferenceManifest,
layoutOrPagePath,
injectedCSSWithCurrentLayout,
injectedJSWithCurrentLayout,
true
)
: { styles: [], scripts: [] }
const preloadedFontFiles = layoutOrPagePath
? getPreloadableFonts(
ctx.renderOpts.nextFontManifest,
layoutOrPagePath,
injectedFontPreloadTagsWithCurrentLayout
)
: null
if (preloadedFontFiles) {
if (preloadedFontFiles.length) {
for (let i = 0; i < preloadedFontFiles.length; i++) {
const fontFilename = preloadedFontFiles[i]
const ext = /\.(woff|woff2|eot|ttf|otf)$/.exec(fontFilename)![1]
const type = `font/${ext}`
const href = `${ctx.assetPrefix}/_next/${encodeURIPath(fontFilename)}`
preloadCallbacks.push(() => {
ctx.componentMod.preloadFont(
href,
type,
ctx.renderOpts.crossOrigin,
ctx.nonce
)
})
}
} else {
try {
let url = new URL(ctx.assetPrefix)
preloadCallbacks.push(() => {
ctx.componentMod.preconnect(url.origin, 'anonymous', ctx.nonce)
})
} catch (error) {
// assetPrefix must not be a fully qualified domain name. We assume
// we should preconnect to same origin instead
preloadCallbacks.push(() => {
ctx.componentMod.preconnect('/', 'anonymous', ctx.nonce)
})
}
}
}
const styles = renderCssResource(styleTags, ctx, preloadCallbacks)
const scripts = scriptTags
? scriptTags.map((href, index) => {
const fullSrc = `${ctx.assetPrefix}/_next/${encodeURIPath(
href
)}${getAssetQueryString(ctx, true)}`
return (
<script
src={fullSrc}
async={true}
key={`script-${index}`}
nonce={ctx.nonce}
/>
)
})
: []
return styles.length || scripts.length ? [...styles, ...scripts] : null
}