51f0359222
Cleans up the code of the middleware SSR loader and the web server. Currently the page components and render options are provided to the server via `globalThis` which is not ideal. Instead we can inject `extendRenderOpts` and `loadComponent` to the web server. Since this is the the minimal mode and we'll need to handle `?flight` requests, we update the server render opts upon `updateRenderOpts` (In the future this should be changed to be passed to `requestHandler` to avoid race conditions). Currently, we can't fully get rid of the `__server_context` global as we call `getBuildId` in the base server constructor. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
121 lines
3.1 KiB
TypeScript
121 lines
3.1 KiB
TypeScript
import {
|
|
BUILD_MANIFEST,
|
|
REACT_LOADABLE_MANIFEST,
|
|
} from '../shared/lib/constants'
|
|
import { join } from 'path'
|
|
import { requirePage } from './require'
|
|
import { BuildManifest } from './get-page-files'
|
|
import { AppType, DocumentType } from '../shared/lib/utils'
|
|
import { interopDefault } from '../lib/interop-default'
|
|
import {
|
|
PageConfig,
|
|
GetStaticPaths,
|
|
GetServerSideProps,
|
|
GetStaticProps,
|
|
} from 'next/types'
|
|
|
|
export type ManifestItem = {
|
|
id: number | string
|
|
files: string[]
|
|
}
|
|
|
|
export type ReactLoadableManifest = { [moduleId: string]: ManifestItem }
|
|
|
|
export type LoadComponentsReturnType = {
|
|
Component: React.ComponentType
|
|
pageConfig: PageConfig
|
|
buildManifest: BuildManifest
|
|
reactLoadableManifest: ReactLoadableManifest
|
|
Document: DocumentType
|
|
App: AppType
|
|
getStaticProps?: GetStaticProps
|
|
getStaticPaths?: GetStaticPaths
|
|
getServerSideProps?: GetServerSideProps
|
|
ComponentMod: any
|
|
}
|
|
|
|
export async function loadDefaultErrorComponents(distDir: string) {
|
|
const Document = interopDefault(require('next/dist/pages/_document'))
|
|
const App = interopDefault(require('next/dist/pages/_app'))
|
|
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,
|
|
}
|
|
}
|
|
|
|
export async function loadComponents(
|
|
distDir: string,
|
|
pathname: string,
|
|
serverless: 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] = await Promise.all([
|
|
requirePage('/_document', distDir, serverless),
|
|
requirePage('/_app', distDir, serverless),
|
|
requirePage(pathname, distDir, serverless),
|
|
])
|
|
|
|
const [buildManifest, reactLoadableManifest] = await Promise.all([
|
|
require(join(distDir, BUILD_MANIFEST)),
|
|
require(join(distDir, REACT_LOADABLE_MANIFEST)),
|
|
])
|
|
|
|
const Component = interopDefault(ComponentMod)
|
|
const Document = interopDefault(DocumentMod)
|
|
const App = interopDefault(AppMod)
|
|
|
|
const { getServerSideProps, getStaticProps, getStaticPaths } = ComponentMod
|
|
|
|
return {
|
|
App,
|
|
Document,
|
|
Component,
|
|
buildManifest,
|
|
reactLoadableManifest,
|
|
pageConfig: ComponentMod.config || {},
|
|
ComponentMod,
|
|
getServerSideProps,
|
|
getStaticProps,
|
|
getStaticPaths,
|
|
}
|
|
}
|