forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
load-components.ts
185 lines (168 loc) · 4.67 KB
/
load-components.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import type {
AppType,
DocumentType,
NextComponentType,
} from '../shared/lib/utils'
import type {
PageConfig,
GetStaticPaths,
GetServerSideProps,
GetStaticProps,
} from 'next/types'
import {
BUILD_MANIFEST,
REACT_LOADABLE_MANIFEST,
MIDDLEWARE_FLIGHT_MANIFEST,
} from '../shared/lib/constants'
import { join } from 'path'
import { requirePage, getPagePath } from './require'
import { BuildManifest } from './get-page-files'
import { interopDefault } from '../lib/interop-default'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
export type ManifestItem = {
id: number | string
files: string[]
}
export type ReactLoadableManifest = { [moduleId: string]: ManifestItem }
export type LoadComponentsReturnType = {
Component: NextComponentType
pageConfig: PageConfig
buildManifest: BuildManifest
reactLoadableManifest: ReactLoadableManifest
serverComponentManifest?: any
Document: DocumentType
App: AppType
getStaticProps?: GetStaticProps
getStaticPaths?: GetStaticPaths
getServerSideProps?: GetServerSideProps
ComponentMod: any
AppMod: any
AppServerMod: any
isViewPath?: boolean
}
export async function loadDefaultErrorComponents(distDir: string) {
const Document = interopDefault(require('next/dist/pages/_document'))
const AppMod = require('next/dist/pages/_app')
const App = interopDefault(AppMod)
const ComponentMod = require('next/dist/pages/_error')
const Component = interopDefault(ComponentMod)
return {
App,
Document,
Component,
pageConfig: {},
buildManifest: require(join(distDir, `fallback-${BUILD_MANIFEST}`)),
reactLoadableManifest: {},
ComponentMod,
AppMod,
// Use App for fallback
AppServerMod: AppMod,
}
}
export async function loadComponents(
distDir: string,
pathname: string,
serverless: boolean,
serverComponents?: boolean,
rootEnabled?: boolean
): Promise<LoadComponentsReturnType> {
if (serverless) {
const ComponentMod = await requirePage(pathname, distDir, serverless)
if (typeof ComponentMod === 'string') {
return {
Component: ComponentMod as any,
pageConfig: {},
ComponentMod,
} as LoadComponentsReturnType
}
let {
default: Component,
getStaticProps,
getStaticPaths,
getServerSideProps,
} = ComponentMod
Component = await Component
getStaticProps = await getStaticProps
getStaticPaths = await getStaticPaths
getServerSideProps = await getServerSideProps
const pageConfig = (await ComponentMod.config) || {}
return {
Component,
pageConfig,
getStaticProps,
getStaticPaths,
getServerSideProps,
ComponentMod,
} as LoadComponentsReturnType
}
const [DocumentMod, AppMod, ComponentMod, AppServerMod] = await Promise.all([
Promise.resolve().then(() =>
requirePage('/_document', distDir, serverless, rootEnabled)
),
Promise.resolve().then(() =>
requirePage('/_app', distDir, serverless, rootEnabled)
),
Promise.resolve().then(() =>
requirePage(pathname, distDir, serverless, rootEnabled)
),
serverComponents
? Promise.resolve().then(() =>
requirePage('/_app.server', distDir, serverless, rootEnabled)
)
: null,
])
const [buildManifest, reactLoadableManifest, serverComponentManifest] =
await Promise.all([
require(join(distDir, BUILD_MANIFEST)),
require(join(distDir, REACT_LOADABLE_MANIFEST)),
serverComponents
? require(join(distDir, 'server', MIDDLEWARE_FLIGHT_MANIFEST + '.json'))
: null,
])
if (serverComponents) {
try {
// Make sure to also load the client entry in cache.
await requirePage(
normalizePagePath(pathname) + '.__sc_client__',
distDir,
serverless,
rootEnabled
)
} catch (_) {
// This page might not be a server component page, so there is no __sc_client__
// bundle to load.
}
}
const Component = interopDefault(ComponentMod)
const Document = interopDefault(DocumentMod)
const App = interopDefault(AppMod)
const { getServerSideProps, getStaticProps, getStaticPaths } = ComponentMod
let isViewPath = false
if (rootEnabled) {
const pagePath = getPagePath(
pathname,
distDir,
serverless,
false,
undefined,
rootEnabled
)
isViewPath = !!pagePath?.match(/server[/\\]views[/\\]/)
}
return {
App,
Document,
Component,
buildManifest,
reactLoadableManifest,
pageConfig: ComponentMod.config || {},
ComponentMod,
AppMod,
AppServerMod,
getServerSideProps,
getStaticProps,
getStaticPaths,
serverComponentManifest,
isViewPath,
}
}