diff --git a/packages/next/export/worker.ts b/packages/next/export/worker.ts index 27da634b5cacf..e3668a16ccb9d 100644 --- a/packages/next/export/worker.ts +++ b/packages/next/export/worker.ts @@ -17,6 +17,7 @@ import { requireFontManifest } from '../next-server/server/require' import { FontManifest } from '../next-server/server/font-utils' import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path' import { trace } from '../telemetry/trace' +import { isInAmpMode } from '../next-server/lib/amp' const envConfig = require('../next-server/lib/runtime-config') @@ -67,8 +68,6 @@ interface RenderOpts { ampPath?: string ampValidatorPath?: string ampSkipValidation?: boolean - hybridAmp?: boolean - inAmpMode?: boolean optimizeFonts?: boolean optimizeImages?: boolean optimizeCss?: any @@ -215,6 +214,8 @@ export default async function exportPage({ let html let curRenderOpts: RenderOpts = {} let renderMethod = renderToHTML + let inAmpMode = false, + hybridAmp = false const renderedDuringBuild = (getStaticProps: any) => { return !buildExport && getStaticProps && !isDynamicRoute(path) @@ -229,11 +230,18 @@ export default async function exportPage({ ...query, }, }) - const { Component: mod, getServerSideProps } = await loadComponents( - distDir, - page, - serverless - ) + const { + Component: mod, + getServerSideProps, + pageConfig, + } = await loadComponents(distDir, page, serverless) + const ampState = { + ampFirst: pageConfig?.amp === true, + hasQuery: Boolean(query.amp), + hybrid: pageConfig?.amp === 'hybrid', + } + inAmpMode = isInAmpMode(ampState) + hybridAmp = ampState.hybrid if (getServerSideProps) { throw new Error( @@ -292,6 +300,13 @@ export default async function exportPage({ } } else { const components = await loadComponents(distDir, page, serverless) + const ampState = { + ampFirst: components.pageConfig?.amp === true, + hasQuery: Boolean(query.amp), + hybrid: components.pageConfig?.amp === 'hybrid', + } + inAmpMode = isInAmpMode(ampState) + hybridAmp = ampState.hybrid if (components.getServerSideProps) { throw new Error( @@ -371,11 +386,11 @@ export default async function exportPage({ } } - if (curRenderOpts.inAmpMode && !curRenderOpts.ampSkipValidation) { + if (inAmpMode && !curRenderOpts.ampSkipValidation) { if (!results.ssgNotFound) { await validateAmp(html, path, curRenderOpts.ampValidatorPath) } - } else if (curRenderOpts.hybridAmp) { + } else if (hybridAmp) { // we need to render the AMP version let ampHtmlFilename = `${ampPath}${sep}index.html` if (!subFolders) { @@ -433,7 +448,7 @@ export default async function exportPage({ 'utf8' ) - if (curRenderOpts.hybridAmp) { + if (hybridAmp) { await promises.writeFile( dataFile.replace(/\.json$/, '.amp.json'), JSON.stringify((curRenderOpts as any).pageData), diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index 9052e38e59fcc..32106aa967b4f 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -165,13 +165,9 @@ export type RenderOptsPartial = { runtimeConfig?: { [key: string]: any } assetPrefix?: string err?: Error | null - autoExport?: boolean nextExport?: boolean dev?: boolean - ampMode?: any ampPath?: string - inAmpMode?: boolean - hybridAmp?: boolean ErrorDebug?: React.ComponentType<{ error: Error }> ampValidator?: (html: string, pathname: string) => Promise ampSkipValidation?: boolean @@ -261,6 +257,7 @@ function renderDocument( devOnlyCacheBusterQueryString: string scriptLoader: any isPreview?: boolean + autoExport?: boolean } ): string { return ( @@ -533,7 +530,6 @@ export async function renderToHTML( req.url!.endsWith('/') && pathname !== '/' && !pageIsDynamic ? '/' : '' }` req.url = pathname - renderOpts.nextExport = true } if (pathname === '/404' && (hasPageGetInitialProps || getServerSideProps)) { @@ -550,8 +546,6 @@ export async function renderToHTML( ) } } - if (isAutoExport) renderOpts.autoExport = true - if (isSSG) renderOpts.nextExport = false await Loadable.preloadAll() // Make sure all dynamic imports are loaded @@ -1038,11 +1032,9 @@ export async function renderToHTML( const dynamicImportsIds = [...dynamicImportIdsSet] const hybridAmp = ampState.hybrid - // update renderOpts so export knows current state - renderOpts.inAmpMode = inAmpMode - renderOpts.hybridAmp = hybridAmp - const docComponentsRendered: DocumentProps['docComponentsRendered'] = {} + const nextExport = + !isSSG && (renderOpts.nextExport || (dev && (isAutoExport || isFallback))) let html = renderDocument(Document, { ...renderOpts, @@ -1078,6 +1070,8 @@ export async function renderToHTML( devOnlyCacheBusterQueryString, scriptLoader, isPreview: isPreview === true ? true : undefined, + autoExport: isAutoExport === true ? true : undefined, + nextExport: nextExport === true ? true : undefined, }) if (process.env.NODE_ENV !== 'production') {