-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathcreate-component-styles-and-scripts.tsx
70 lines (63 loc) · 2.08 KB
/
create-component-styles-and-scripts.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
import React from 'react'
import { interopDefault } from './interop-default'
import { getLinkAndScriptTags } from './get-css-inlined-link-tags'
import type { AppRenderContext } from './app-render'
import { getAssetQueryString } from './get-asset-query-string'
import { encodeURIPath } from '../../shared/lib/encode-uri-path'
export async function createComponentStylesAndScripts({
filePath,
getComponent,
injectedCSS,
injectedJS,
ctx,
}: {
filePath: string
getComponent: () => any
injectedCSS: Set<string>
injectedJS: Set<string>
ctx: AppRenderContext
}): Promise<[React.ComponentType<any>, React.ReactNode, React.ReactNode]> {
const { styles: cssHrefs, scripts: jsHrefs } = getLinkAndScriptTags(
ctx.clientReferenceManifest,
filePath,
injectedCSS,
injectedJS
)
const styles = cssHrefs
? cssHrefs.map((href, index) => {
const fullHref = `${ctx.assetPrefix}/_next/${encodeURIPath(
href
)}${getAssetQueryString(ctx, true)}`
// `Precedence` is an opt-in signal for React to handle resource
// loading and deduplication, etc. It's also used as the key to sort
// resources so they will be injected in the correct order.
// During HMR, it's critical to use different `precedence` values
// for different stylesheets, so their order will be kept.
// https://github.com/facebook/react/pull/25060
const precedence =
process.env.NODE_ENV === 'development' ? 'next_' + href : 'next'
return (
<link
rel="stylesheet"
href={fullHref}
// @ts-ignore
precedence={precedence}
crossOrigin={ctx.renderOpts.crossOrigin}
key={index}
/>
)
})
: null
const scripts = jsHrefs
? jsHrefs.map((href) => (
<script
src={`${ctx.assetPrefix}/_next/${encodeURIPath(
href
)}${getAssetQueryString(ctx, true)}`}
async={true}
/>
))
: null
const Comp = interopDefault(await getComponent())
return [Comp, styles, scripts]
}