diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index 89617e2e609e9..4f9336efd5b91 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -80,9 +80,6 @@ import { collectPages, detectConflictingPaths, getJsPageSizeInKb, - getNamedExports, - hasCustomGetInitialProps, - isPageStatic, PageInfo, printCustomRoutes, printTreeView, @@ -265,7 +262,7 @@ export default async function build( ) const pageKeys = Object.keys(mappedPages) const conflictingPublicFiles: string[] = [] - const hasCustomErrorPage = mappedPages['/_error'].startsWith( + const hasCustomErrorPage: boolean = mappedPages['/_error'].startsWith( 'private-next-pages' ) const hasPages404 = Boolean( @@ -655,212 +652,221 @@ export default async function build( await promises.readFile(buildManifestPath, 'utf8') ) as BuildManifest - let customAppGetInitialProps: boolean | undefined - let namedExports: Array | undefined - let isNextImageImported: boolean | undefined const analysisBegin = process.hrtime() - let hasSsrAmpPages = false const staticCheckSpan = nextBuildSpan.traceChild('static-check') - const { hasNonStaticErrorPage } = await staticCheckSpan.traceAsyncFn( - async () => { - process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD + const { + customAppGetInitialProps, + namedExports, + isNextImageImported, + hasSsrAmpPages, + hasNonStaticErrorPage, + } = await staticCheckSpan.traceAsyncFn(async () => { + process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD + + const staticCheckWorkers = new Worker(staticCheckWorker, { + numWorkers: config.experimental.cpus, + enableWorkerThreads: config.experimental.workerThreads, + }) as Worker & typeof import('./utils') + + staticCheckWorkers.getStdout().pipe(process.stdout) + staticCheckWorkers.getStderr().pipe(process.stderr) + + const runtimeEnvConfig = { + publicRuntimeConfig: config.publicRuntimeConfig, + serverRuntimeConfig: config.serverRuntimeConfig, + } - const staticCheckWorkers = new Worker(staticCheckWorker, { - numWorkers: config.experimental.cpus, - enableWorkerThreads: config.experimental.workerThreads, - }) as Worker & { isPageStatic: typeof isPageStatic } + const nonStaticErrorPageSpan = staticCheckSpan.traceChild( + 'check-static-error-page' + ) + const nonStaticErrorPagePromise = nonStaticErrorPageSpan.traceAsyncFn( + async () => + hasCustomErrorPage && + (await staticCheckWorkers.hasCustomGetInitialProps( + '/_error', + distDir, + isLikeServerless, + runtimeEnvConfig, + false + )) + ) + // we don't output _app in serverless mode so use _app export + // from _error instead + const appPageToCheck = isLikeServerless ? '/_error' : '/_app' + + const customAppGetInitialPropsPromise = staticCheckWorkers.hasCustomGetInitialProps( + appPageToCheck, + distDir, + isLikeServerless, + runtimeEnvConfig, + true + ) - staticCheckWorkers.getStdout().pipe(process.stdout) - staticCheckWorkers.getStderr().pipe(process.stderr) + const namedExportsPromise = staticCheckWorkers.getNamedExports( + appPageToCheck, + distDir, + isLikeServerless, + runtimeEnvConfig + ) - const runtimeEnvConfig = { - publicRuntimeConfig: config.publicRuntimeConfig, - serverRuntimeConfig: config.serverRuntimeConfig, - } + // eslint-disable-next-line no-shadow + let isNextImageImported: boolean | undefined + // eslint-disable-next-line no-shadow + let hasSsrAmpPages = false - const nonStaticErrorPageSpan = staticCheckSpan.traceChild( - 'check-static-error-page' - ) - const nonStaticErrorPage = await nonStaticErrorPageSpan.traceAsyncFn( - async () => - hasCustomErrorPage && - (await hasCustomGetInitialProps( - '/_error', + await Promise.all( + pageKeys.map(async (page) => { + const checkPageSpan = staticCheckSpan.traceChild('check-page', { + page, + }) + return checkPageSpan.traceAsyncFn(async () => { + const actualPage = normalizePagePath(page) + const [selfSize, allSize] = await getJsPageSizeInKb( + actualPage, distDir, - isLikeServerless, - runtimeEnvConfig, - false - )) - ) - // we don't output _app in serverless mode so use _app export - // from _error instead - const appPageToCheck = isLikeServerless ? '/_error' : '/_app' + buildManifest + ) - customAppGetInitialProps = await hasCustomGetInitialProps( - appPageToCheck, - distDir, - isLikeServerless, - runtimeEnvConfig, - true - ) + let isSsg = false + let isStatic = false + let isHybridAmp = false + let ssgPageRoutes: string[] | null = null - namedExports = await getNamedExports( - appPageToCheck, - distDir, - isLikeServerless, - runtimeEnvConfig - ) + const nonReservedPage = !page.match( + /^\/(_app|_error|_document|api(\/|$))/ + ) - 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://nextjs.org/docs/messages/opt-out-auto-static-optimization\n' - ) - } + if (nonReservedPage) { + try { + let isPageStaticSpan = checkPageSpan.traceChild( + 'is-page-static' + ) + let workerResult = await isPageStaticSpan.traceAsyncFn(() => { + return staticCheckWorkers.isPageStatic( + page, + distDir, + isLikeServerless, + runtimeEnvConfig, + config.i18n?.locales, + config.i18n?.defaultLocale, + isPageStaticSpan.id + ) + }) - await Promise.all( - pageKeys.map(async (page) => { - const checkPageSpan = staticCheckSpan.traceChild('check-page', { - page, - }) - return checkPageSpan.traceAsyncFn(async () => { - const actualPage = normalizePagePath(page) - const [selfSize, allSize] = await getJsPageSizeInKb( - actualPage, - distDir, - buildManifest - ) + if ( + workerResult.isStatic === false && + (workerResult.isHybridAmp || workerResult.isAmpOnly) + ) { + hasSsrAmpPages = true + } - let isSsg = false - let isStatic = false - let isHybridAmp = false - let ssgPageRoutes: string[] | null = null + if (workerResult.isHybridAmp) { + isHybridAmp = true + hybridAmpPages.add(page) + } - const nonReservedPage = !page.match( - /^\/(_app|_error|_document|api(\/|$))/ - ) + if (workerResult.isNextImageImported) { + isNextImageImported = true + } - if (nonReservedPage) { - try { - let isPageStaticSpan = checkPageSpan.traceChild( - 'is-page-static' - ) - let workerResult = await isPageStaticSpan.traceAsyncFn(() => { - return staticCheckWorkers.isPageStatic( - page, - distDir, - isLikeServerless, - runtimeEnvConfig, - config.i18n?.locales, - config.i18n?.defaultLocale, - isPageStaticSpan.id - ) - }) + if (workerResult.hasStaticProps) { + ssgPages.add(page) + isSsg = true if ( - workerResult.isStatic === false && - (workerResult.isHybridAmp || workerResult.isAmpOnly) + workerResult.prerenderRoutes && + workerResult.encodedPrerenderRoutes ) { - hasSsrAmpPages = true - } - - if (workerResult.isHybridAmp) { - isHybridAmp = true - hybridAmpPages.add(page) - } - - if (workerResult.isNextImageImported) { - isNextImageImported = true - } - - if (workerResult.hasStaticProps) { - ssgPages.add(page) - isSsg = true - - if ( - workerResult.prerenderRoutes && + additionalSsgPaths.set(page, workerResult.prerenderRoutes) + additionalSsgPathsEncoded.set( + page, workerResult.encodedPrerenderRoutes - ) { - additionalSsgPaths.set(page, workerResult.prerenderRoutes) - additionalSsgPathsEncoded.set( - page, - workerResult.encodedPrerenderRoutes - ) - ssgPageRoutes = workerResult.prerenderRoutes - } - - if (workerResult.prerenderFallback === 'blocking') { - ssgBlockingFallbackPages.add(page) - } else if (workerResult.prerenderFallback === true) { - ssgStaticFallbackPages.add(page) - } - } else if (workerResult.hasServerProps) { - serverPropsPages.add(page) - } else if ( - workerResult.isStatic && - customAppGetInitialProps === false - ) { - staticPages.add(page) - isStatic = true + ) + ssgPageRoutes = workerResult.prerenderRoutes } - if (hasPages404 && page === '/404') { - if ( - !workerResult.isStatic && - !workerResult.hasStaticProps - ) { - throw new Error( - `\`pages/404\` ${STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR}` - ) - } - // we need to ensure the 404 lambda is present since we use - // it when _app has getInitialProps - if ( - customAppGetInitialProps && - !workerResult.hasStaticProps - ) { - staticPages.delete(page) - } + if (workerResult.prerenderFallback === 'blocking') { + ssgBlockingFallbackPages.add(page) + } else if (workerResult.prerenderFallback === true) { + ssgStaticFallbackPages.add(page) } + } else if (workerResult.hasServerProps) { + serverPropsPages.add(page) + } else if ( + workerResult.isStatic && + (await customAppGetInitialPropsPromise) === false + ) { + staticPages.add(page) + isStatic = true + } + if (hasPages404 && page === '/404') { + if (!workerResult.isStatic && !workerResult.hasStaticProps) { + throw new Error( + `\`pages/404\` ${STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR}` + ) + } + // we need to ensure the 404 lambda is present since we use + // it when _app has getInitialProps if ( - STATIC_STATUS_PAGES.includes(page) && - !workerResult.isStatic && + (await customAppGetInitialPropsPromise) && !workerResult.hasStaticProps ) { - throw new Error( - `\`pages${page}\` ${STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR}` - ) + staticPages.delete(page) } - } catch (err) { - if (err.message !== 'INVALID_DEFAULT_EXPORT') throw err - invalidPages.add(page) } + + if ( + STATIC_STATUS_PAGES.includes(page) && + !workerResult.isStatic && + !workerResult.hasStaticProps + ) { + throw new Error( + `\`pages${page}\` ${STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR}` + ) + } + } catch (err) { + if (err.message !== 'INVALID_DEFAULT_EXPORT') throw err + invalidPages.add(page) } + } - pageInfos.set(page, { - size: selfSize, - totalSize: allSize, - static: isStatic, - isSsg, - isHybridAmp, - ssgPageRoutes, - initialRevalidateSeconds: false, - }) + pageInfos.set(page, { + size: selfSize, + totalSize: allSize, + static: isStatic, + isSsg, + isHybridAmp, + ssgPageRoutes, + initialRevalidateSeconds: false, }) }) - ) - staticCheckWorkers.end() - - return { hasNonStaticErrorPage: nonStaticErrorPage } + }) + ) + const returnValue = { + customAppGetInitialProps: await customAppGetInitialPropsPromise, + namedExports: await namedExportsPromise, + isNextImageImported, + hasSsrAmpPages, + hasNonStaticErrorPage: await nonStaticErrorPagePromise, } - ) + + staticCheckWorkers.end() + return returnValue + }) + + 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://nextjs.org/docs/messages/opt-out-auto-static-optimization\n' + ) + } if (!hasSsrAmpPages) { requiredServerFiles.ignore.push(