Skip to content

Commit

Permalink
use requirePage for /_document and /_app (#14176)
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens authored Jun 15, 2020
1 parent d68afed commit ea6b8da
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 104 deletions.
86 changes: 31 additions & 55 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import {
import getBaseWebpackConfig from './webpack-config'
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
import { writeBuildId } from './write-build-id'

import { getPagePath } from '../next-server/server/require'
const staticCheckWorker = require.resolve('./utils')

export type SsgRoute = {
Expand Down Expand Up @@ -481,13 +481,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
hasNonStaticErrorPage =
hasCustomErrorPage &&
(await hasCustomGetInitialProps(
path.join(
distDir,
...(isLikeServerless
? ['serverless', 'pages']
: ['server', 'static', buildId, 'pages']),
'_error.js'
),
getPagePath('/_error', distDir, isLikeServerless),
runtimeEnvConfig,
false
))
Expand All @@ -502,64 +496,47 @@ export default async function build(dir: string, conf = null): Promise<void> {
buildManifest,
config.experimental.modern
)
const bundleRelative = path.join(
isLikeServerless ? 'pages' : `static/${buildId}/pages`,
actualPage + '.js'
)
const serverBundle = path.join(
distDir,
isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
bundleRelative
)

let isSsg = false
let isStatic = false
let isHybridAmp = false
let ssgPageRoutes: string[] | null = null
let hasSsgFallback: boolean = false

pagesManifest[page] = bundleRelative.replace(/\\/g, '/')

const nonReservedPage = !page.match(/^\/(_app|_error|_document|api)/)

if (nonReservedPage && customAppGetInitialProps === undefined) {
customAppGetInitialProps = hasCustomGetInitialProps(
isLikeServerless
? serverBundle
: path.join(
distDir,
SERVER_DIRECTORY,
`/static/${buildId}/pages/_app.js`
),
runtimeEnvConfig,
true
)

namedExports = getNamedExports(
isLikeServerless
? serverBundle
: path.join(
distDir,
SERVER_DIRECTORY,
`/static/${buildId}/pages/_app.js`
),
runtimeEnvConfig
)

if (customAppGetInitialProps) {
console.warn(
chalk.bold.yellow(`Warning: `) +
chalk.yellow(
`You have opted-out of Automatic Static Optimization due to \`getInitialProps\` in \`pages/_app\`. This does not opt-out pages with \`getStaticProps\``
)
if (nonReservedPage) {
const serverBundle = getPagePath(page, distDir, isLikeServerless)

if (customAppGetInitialProps === undefined) {
customAppGetInitialProps = hasCustomGetInitialProps(
isLikeServerless
? serverBundle
: getPagePath('/_app', distDir, isLikeServerless),
runtimeEnvConfig,
true
)
console.warn(
'Read more: https://err.sh/next.js/opt-out-auto-static-optimization\n'

namedExports = getNamedExports(
isLikeServerless
? serverBundle
: getPagePath('/_app', distDir, isLikeServerless),
runtimeEnvConfig
)

if (customAppGetInitialProps) {
console.warn(
chalk.bold.yellow(`Warning: `) +
chalk.yellow(
`You have opted-out of Automatic Static Optimization due to \`getInitialProps\` in \`pages/_app\`. This does not opt-out pages with \`getStaticProps\``
)
)
console.warn(
'Read more: https://err.sh/next.js/opt-out-auto-static-optimization\n'
)
}
}
}

if (nonReservedPage) {
try {
let workerResult = await staticCheckWorkers.isPageStatic(
page,
Expand Down Expand Up @@ -613,7 +590,6 @@ export default async function build(dir: string, conf = null): Promise<void> {
pageInfos.set(page, {
size: selfSize,
totalSize: allSize,
serverBundle,
static: isStatic,
isSsg,
isHybridAmp,
Expand Down Expand Up @@ -762,7 +738,7 @@ export default async function build(dir: string, conf = null): Promise<void> {

// remove server bundles that were exported
for (const page of staticPages) {
const { serverBundle } = pageInfos.get(page)!
const serverBundle = getPagePath(page, distDir, isLikeServerless)
await promises.unlink(serverBundle)
}

Expand Down
1 change: 0 additions & 1 deletion packages/next/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export interface PageInfo {
isSsg: boolean
ssgPageRoutes: string[] | null
hasSsgFallback: boolean
serverBundle: string
}

export async function printTreeView(
Expand Down
1 change: 0 additions & 1 deletion packages/next/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ export default async function exportApp(
path,
pathMap: exportPathMap[path],
distDir,
buildId,
outDir,
pagesDataDir,
renderOpts,
Expand Down
14 changes: 3 additions & 11 deletions packages/next/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ interface PathMap {
query?: { [key: string]: string | string[] }
}

interface ExortPageInput {
interface ExportPageInput {
path: string
pathMap: PathMap
distDir: string
buildId: string
outDir: string
pagesDataDir: string
renderOpts: RenderOpts
Expand Down Expand Up @@ -72,15 +71,14 @@ export default async function exportPage({
path,
pathMap,
distDir,
buildId,
outDir,
pagesDataDir,
renderOpts,
buildExport,
serverRuntimeConfig,
subFolders,
serverless,
}: ExortPageInput): Promise<ExportPageResults> {
}: ExportPageInput): Promise<ExportPageResults> {
let results: ExportPageResults = {
ampValidations: [],
}
Expand Down Expand Up @@ -181,7 +179,6 @@ export default async function exportPage({
})
const { Component: mod, getServerSideProps } = await loadComponents(
distDir,
buildId,
page,
serverless
)
Expand Down Expand Up @@ -226,12 +223,7 @@ export default async function exportPage({
throw new Error(`Failed to render serverless page`)
}
} else {
const components = await loadComponents(
distDir,
buildId,
page,
serverless
)
const components = await loadComponents(distDir, page, serverless)

if (components.getServerSideProps) {
throw new Error(`Error for page ${page}: ${SERVER_PROPS_EXPORT_ERROR}`)
Expand Down
30 changes: 3 additions & 27 deletions packages/next/next-server/server/load-components.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
BUILD_MANIFEST,
CLIENT_STATIC_FILES_PATH,
REACT_LOADABLE_MANIFEST,
SERVER_DIRECTORY,
} from '../lib/constants'
import { BUILD_MANIFEST, REACT_LOADABLE_MANIFEST } from '../lib/constants'
import { join } from 'path'
import { requirePage } from './require'
import { BuildManifest } from './get-page-files'
Expand Down Expand Up @@ -41,7 +36,6 @@ export type LoadComponentsReturnType = {

export async function loadComponents(
distDir: string,
buildId: string,
pathname: string,
serverless: boolean
): Promise<LoadComponentsReturnType> {
Expand All @@ -57,27 +51,9 @@ export async function loadComponents(
getServerSideProps,
} as LoadComponentsReturnType
}
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 AppMod = require(appPath)

const DocumentMod = requirePage('/_document', distDir, serverless)
const AppMod = requirePage('/_app', distDir, serverless)
const ComponentMod = requirePage(pathname, distDir, serverless)

const [
Expand Down
2 changes: 0 additions & 2 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,6 @@ export default class Server {
try {
const components = await loadComponents(
this.distDir,
this.buildId,
pagePath!,
!this.renderOpts.dev && this._isLikeServerless
)
Expand Down Expand Up @@ -909,7 +908,6 @@ export default class Server {
const __getStaticPaths = async () => {
const paths = await this.staticPathsWorker!.loadStaticPaths(
this.distDir,
this.buildId,
pathname,
!this.renderOpts.dev && this._isLikeServerless
)
Expand Down
8 changes: 1 addition & 7 deletions packages/next/server/static-paths-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ let workerWasUsed = false
// during a production build
export async function loadStaticPaths(
distDir: string,
buildId: string,
pathname: string,
serverless: boolean
) {
Expand All @@ -19,12 +18,7 @@ export async function loadStaticPaths(
process.exit(1)
}

const components = await loadComponents(
distDir,
buildId,
pathname,
serverless
)
const components = await loadComponents(distDir, pathname, serverless)

if (!components.getStaticPaths) {
// we shouldn't get to this point since the worker should
Expand Down

0 comments on commit ea6b8da

Please sign in to comment.