rsnext/packages/next-server/server/load-components.ts
JJ Kasper 0ca8087565
Add prerender PageConfig option (#7699)
* Add prerender PageConfig option

* Update PageConfig type

* Add inlining of data when pre-render is set and add tests

* Update types import

* Add check for props

* Rename prerender to experimentalPrerender for now
2019-07-01 14:13:52 -07:00

80 lines
1.7 KiB
TypeScript

import {
BUILD_MANIFEST,
CLIENT_STATIC_FILES_PATH,
REACT_LOADABLE_MANIFEST,
SERVER_DIRECTORY,
} from '../lib/constants'
import { join } from 'path'
import { PageConfig } from 'next-server/types'
import { requirePage } from './require'
export function interopDefault(mod: any) {
return mod.default || mod
}
export type LoadComponentsReturnType = {
Component: any
pageConfig: PageConfig
buildManifest?: any
reactLoadableManifest?: any
Document?: any
DocumentMiddleware?: any
App?: any
}
export async function loadComponents(
distDir: string,
buildId: string,
pathname: string,
serverless: boolean
): Promise<LoadComponentsReturnType> {
if (serverless) {
const Component = await requirePage(pathname, distDir, serverless)
return { Component, pageConfig: Component.config || {} }
}
const documentPath = join(
distDir,
SERVER_DIRECTORY,
CLIENT_STATIC_FILES_PATH,
buildId,
'pages',
'_document'
)
const appPath = join(
distDir,
SERVER_DIRECTORY,
CLIENT_STATIC_FILES_PATH,
buildId,
'pages',
'_app'
)
const DocumentMod = require(documentPath)
const { middleware: DocumentMiddleware } = DocumentMod
const ComponentMod = requirePage(pathname, distDir, serverless)
const [
buildManifest,
reactLoadableManifest,
Component,
Document,
App,
] = await Promise.all([
require(join(distDir, BUILD_MANIFEST)),
require(join(distDir, REACT_LOADABLE_MANIFEST)),
interopDefault(ComponentMod),
interopDefault(DocumentMod),
interopDefault(require(appPath)),
])
return {
App,
Document,
Component,
buildManifest,
DocumentMiddleware,
reactLoadableManifest,
pageConfig: ComponentMod.config || {},
}
}