From 132e5e9396547cf9094015dbaa1f2ad2223f7f1e Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Fri, 21 Oct 2022 16:38:36 +0200 Subject: [PATCH 1/4] add initial handling --- packages/next/build/index.ts | 234 ++++++++++++++++++----------------- packages/next/build/utils.ts | 4 +- 2 files changed, 121 insertions(+), 117 deletions(-) diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index f5bee9cdc7f73..b41f61a43d3af 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -1365,148 +1365,152 @@ export default async function build( if (pageType === 'app' && originalAppPath) { appNormalizedPaths.set(originalAppPath, page) - // TODO-APP: handle prerendering with edge - // runtime if (pageRuntime === 'experimental-edge') { - return - } - - if ( - workerResult.encodedPrerenderRoutes && - workerResult.prerenderRoutes - ) { - appStaticPaths.set( - originalAppPath, + isStatic = false + isSsg = false + } else { + console.log('workerResult', workerResult, page) + if ( + workerResult.encodedPrerenderRoutes && workerResult.prerenderRoutes - ) - appStaticPathsEncoded.set( + ) { + appStaticPaths.set( + originalAppPath, + workerResult.prerenderRoutes + ) + appStaticPathsEncoded.set( + originalAppPath, + workerResult.encodedPrerenderRoutes + ) + isSsg = true + } + if ( + !isDynamicRoute(page) && + workerResult.appConfig?.revalidate !== 0 + ) { + appStaticPaths.set(originalAppPath, [page]) + appStaticPathsEncoded.set(originalAppPath, [page]) + isStatic = true + } + if (workerResult.prerenderFallback) { + // whether or not to allow requests for paths not + // returned from generateStaticParams + appDynamicParamPaths.add(originalAppPath) + } + appDefaultConfigs.set( originalAppPath, - workerResult.encodedPrerenderRoutes + workerResult.appConfig || {} ) } - if (!isDynamicRoute(page)) { - appStaticPaths.set(originalAppPath, [page]) - appStaticPathsEncoded.set(originalAppPath, [page]) - } - if (workerResult.prerenderFallback) { - // whether or not to allow requests for paths not - // returned from generateStaticParams - appDynamicParamPaths.add(originalAppPath) + } else { + if (pageRuntime === SERVER_RUNTIME.edge) { + if (workerResult.hasStaticProps) { + console.warn( + `"getStaticProps" is not yet supported fully with "experimental-edge", detected on ${page}` + ) + } + // TODO: add handling for statically rendering edge + // pages and allow edge with Prerender outputs + workerResult.isStatic = false + workerResult.hasStaticProps = false } - appDefaultConfigs.set( - originalAppPath, - workerResult.appConfig || {} - ) - return - } - if (pageRuntime === SERVER_RUNTIME.edge) { - if (workerResult.hasStaticProps) { - console.warn( - `"getStaticProps" is not yet supported fully with "experimental-edge", detected on ${page}` + if (config.outputFileTracing) { + pageTraceIncludes.set( + page, + workerResult.traceIncludes || [] + ) + pageTraceExcludes.set( + page, + workerResult.traceExcludes || [] ) } - // TODO: add handling for statically rendering edge - // pages and allow edge with Prerender outputs - workerResult.isStatic = false - workerResult.hasStaticProps = false - } - if (config.outputFileTracing) { - pageTraceIncludes.set( - page, - workerResult.traceIncludes || [] - ) - pageTraceExcludes.set( - page, - workerResult.traceExcludes || [] - ) - } - - if ( - workerResult.isStatic === false && - (workerResult.isHybridAmp || workerResult.isAmpOnly) - ) { - hasSsrAmpPages = true - } + if ( + workerResult.isStatic === false && + (workerResult.isHybridAmp || workerResult.isAmpOnly) + ) { + hasSsrAmpPages = true + } - if (workerResult.isHybridAmp) { - isHybridAmp = true - hybridAmpPages.add(page) - } + if (workerResult.isHybridAmp) { + isHybridAmp = true + hybridAmpPages.add(page) + } - if (workerResult.isNextImageImported) { - isNextImageImported = true - } + if (workerResult.isNextImageImported) { + isNextImageImported = true + } - if (workerResult.hasStaticProps) { - ssgPages.add(page) - isSsg = true + if (workerResult.hasStaticProps) { + ssgPages.add(page) + isSsg = true - if ( - workerResult.prerenderRoutes && - workerResult.encodedPrerenderRoutes - ) { - additionalSsgPaths.set( - page, - workerResult.prerenderRoutes - ) - additionalSsgPathsEncoded.set( - page, + if ( + workerResult.prerenderRoutes && workerResult.encodedPrerenderRoutes - ) - ssgPageRoutes = workerResult.prerenderRoutes + ) { + 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 && + !isServerComponent && + (await customAppGetInitialPropsPromise) === false + ) { + staticPages.add(page) + isStatic = true + } else if (isServerComponent) { + // This is a static server component page that doesn't have + // gSP or gSSP. We still treat it as a SSG page. + ssgPages.add(page) + isSsg = true } - if (workerResult.prerenderFallback === 'blocking') { - ssgBlockingFallbackPages.add(page) - } else if (workerResult.prerenderFallback === true) { - ssgStaticFallbackPages.add(page) + 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 ( + (await customAppGetInitialPropsPromise) && + !workerResult.hasStaticProps + ) { + staticPages.delete(page) + } } - } else if (workerResult.hasServerProps) { - serverPropsPages.add(page) - } else if ( - workerResult.isStatic && - !isServerComponent && - (await customAppGetInitialPropsPromise) === false - ) { - staticPages.add(page) - isStatic = true - } else if (isServerComponent) { - // This is a static server component page that doesn't have - // gSP or gSSP. We still treat it as a SSG page. - ssgPages.add(page) - isSsg = true - } - if (hasPages404 && page === '/404') { if ( + STATIC_STATUS_PAGES.includes(page) && !workerResult.isStatic && !workerResult.hasStaticProps ) { throw new Error( - `\`pages/404\` ${STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR}` + `\`pages${page}\` ${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 ( - (await customAppGetInitialPropsPromise) && - !workerResult.hasStaticProps - ) { - staticPages.delete(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 ( diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index 8165f4a417f21..d71afc33aeef2 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -412,7 +412,7 @@ export async function printTreeView( (pageInfo?.ssgPageDurations?.reduce((a, b) => a + (b || 0), 0) || 0) const symbol = - routerType === 'app' || item === '/_app' || item === '/_app.server' + item === '/_app' || item === '/_app.server' ? ' ' : pageInfo?.static ? '○' @@ -427,7 +427,7 @@ export async function printTreeView( if (pageInfo?.initialRevalidateSeconds) usedSymbols.add('ISR') messages.push([ - `${border} ${routerType === 'pages' ? `${symbol} ` : ''}${ + `${border} ${symbol} ${ pageInfo?.initialRevalidateSeconds ? `${item} (ISR: ${pageInfo?.initialRevalidateSeconds} Seconds)` : item From 64e1ae4dfd7caed26d1842c460c186e477123c7e Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Fri, 21 Oct 2022 16:42:23 +0200 Subject: [PATCH 2/4] remove log --- packages/next/build/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index b41f61a43d3af..0df871e0d6186 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -1370,7 +1370,6 @@ export default async function build( isStatic = false isSsg = false } else { - console.log('workerResult', workerResult, page) if ( workerResult.encodedPrerenderRoutes && workerResult.prerenderRoutes From d17d3caad8d91c76735e17974dc66cecc08d79b0 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Fri, 21 Oct 2022 17:04:52 +0200 Subject: [PATCH 3/4] add SSG'd routes --- packages/next/build/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index 0df871e0d6186..0a73d85e103f8 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -1382,6 +1382,7 @@ export default async function build( originalAppPath, workerResult.encodedPrerenderRoutes ) + ssgPageRoutes = workerResult.prerenderRoutes isSsg = true } if ( From 55cfc58e7bf328a9c84c250547268da863038196 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Fri, 21 Oct 2022 17:42:01 +0200 Subject: [PATCH 4/4] fix dynamic fetches --- packages/next/build/index.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/next/build/index.ts b/packages/next/build/index.ts index 0a73d85e103f8..ef72eec559b48 100644 --- a/packages/next/build/index.ts +++ b/packages/next/build/index.ts @@ -2155,6 +2155,13 @@ export default async function build( } } else { hasDynamicData = true + // we might have determined during prerendering that this page + // used dynamic data + pageInfos.set(route, { + ...(pageInfos.get(route) as PageInfo), + isSsg: false, + static: false, + }) } })