-
Notifications
You must be signed in to change notification settings - Fork 26k
/
render.ts
152 lines (141 loc) · 4.37 KB
/
render.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
import type { NextConfigComplete } from '../../../../server/config-shared'
import type { DocumentType, AppType } from '../../../../shared/lib/utils'
import type { BuildManifest } from '../../../../server/get-page-files'
import type { ReactLoadableManifest } from '../../../../server/load-components'
import type { ClientReferenceManifest } from '../../plugins/flight-manifest-plugin'
import type { NextFontManifestPlugin } from '../../plugins/next-font-manifest-plugin'
import WebServer from '../../../../server/web-server'
import {
WebNextRequest,
WebNextResponse,
} from '../../../../server/base-http/web'
import { SERVER_RUNTIME } from '../../../../lib/constants'
import { PrerenderManifest } from '../../..'
import { normalizeAppPath } from '../../../../shared/lib/router/utils/app-paths'
import { SizeLimit } from '../../../../../types'
export function getRender({
dev,
page,
appMod,
pageMod,
errorMod,
error500Mod,
pagesType,
Document,
buildManifest,
prerenderManifest,
reactLoadableManifest,
renderToHTML,
clientReferenceManifest,
subresourceIntegrityManifest,
serverActionsManifest,
serverActionsBodySizeLimit,
config,
buildId,
nextFontManifest,
incrementalCacheHandler,
}: {
pagesType: 'app' | 'pages' | 'root'
dev: boolean
page: string
appMod: any
pageMod: any
errorMod: any
error500Mod: any
renderToHTML: any
Document: DocumentType
buildManifest: BuildManifest
prerenderManifest: PrerenderManifest
reactLoadableManifest: ReactLoadableManifest
subresourceIntegrityManifest?: Record<string, string>
clientReferenceManifest?: ClientReferenceManifest
serverActionsManifest: any
serverActionsBodySizeLimit?: SizeLimit
appServerMod: any
config: NextConfigComplete
buildId: string
nextFontManifest: NextFontManifestPlugin
incrementalCacheHandler?: any
}) {
const isAppPath = pagesType === 'app'
const baseLoadComponentResult = {
dev,
buildManifest,
prerenderManifest,
reactLoadableManifest,
subresourceIntegrityManifest,
nextFontManifest,
Document,
App: appMod?.default as AppType,
}
const server = new WebServer({
dev,
conf: config,
minimalMode: true,
webServerConfig: {
page,
normalizedPage: isAppPath ? normalizeAppPath(page) : page,
pagesType,
prerenderManifest,
extendRenderOpts: {
buildId,
runtime: SERVER_RUNTIME.experimentalEdge,
supportsDynamicHTML: true,
disableOptimizedLoading: true,
clientReferenceManifest,
serverActionsManifest,
serverActionsBodySizeLimit,
},
renderToHTML,
incrementalCacheHandler,
loadComponent: async (pathname) => {
if (pathname === page) {
return {
...baseLoadComponentResult,
Component: pageMod.default,
pageConfig: pageMod.config || {},
getStaticProps: pageMod.getStaticProps,
getServerSideProps: pageMod.getServerSideProps,
getStaticPaths: pageMod.getStaticPaths,
ComponentMod: pageMod,
isAppPath: !!pageMod.__next_app__,
pathname,
}
}
// If there is a custom 500 page, we need to handle it separately.
if (pathname === '/500' && error500Mod) {
return {
...baseLoadComponentResult,
Component: error500Mod.default,
pageConfig: error500Mod.config || {},
getStaticProps: error500Mod.getStaticProps,
getServerSideProps: error500Mod.getServerSideProps,
getStaticPaths: error500Mod.getStaticPaths,
ComponentMod: error500Mod,
pathname,
}
}
if (pathname === '/_error') {
return {
...baseLoadComponentResult,
Component: errorMod.default,
pageConfig: errorMod.config || {},
getStaticProps: errorMod.getStaticProps,
getServerSideProps: errorMod.getServerSideProps,
getStaticPaths: errorMod.getStaticPaths,
ComponentMod: errorMod,
pathname,
}
}
return null
},
},
})
const handler = server.getRequestHandler()
return async function render(request: Request) {
const extendedReq = new WebNextRequest(request)
const extendedRes = new WebNextResponse()
handler(extendedReq, extendedRes)
return await extendedRes.toResponse()
}
}