From 931a0ea338e29059c089e5db5b5405baa82e4b87 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Wed, 22 Feb 2023 21:17:21 +0100 Subject: [PATCH 1/3] skip prerendering default slug --- packages/next/src/build/index.ts | 27 +++++++++++++------ .../e2e/app-dir/app-static/app-static.test.ts | 8 ------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index d4255c6735595..7773c6f70593b 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -1434,15 +1434,26 @@ export default async function build( ssgPageRoutes = workerResult.prerenderRoutes isSsg = true } - if ( - (!isDynamicRoute(page) || - !workerResult.prerenderRoutes?.length) && - workerResult.appConfig?.revalidate !== 0 - ) { - appStaticPaths.set(originalAppPath, [page]) - appStaticPathsEncoded.set(originalAppPath, [page]) - isStatic = true + + const isDynamic = isDynamicRoute(page) + const isDynamicPageWithoutGenerateStaticParams = + isDynamic && !workerResult.prerenderRoutes + + if (workerResult.appConfig?.revalidate !== 0) { + if (!isDynamic) { + appStaticPaths.set(originalAppPath, [page]) + appStaticPathsEncoded.set(originalAppPath, [page]) + isStatic = true + } else if (isDynamicPageWithoutGenerateStaticParams) { + // Do not prerender with the empty slug name if there + // is no generateStaticParams provided. + // https://github.com/vercel/next.js/issues/45850 + appStaticPaths.set(originalAppPath, []) + appStaticPathsEncoded.set(originalAppPath, []) + isStatic = true + } } + if (workerResult.prerenderFallback) { // whether or not to allow requests for paths not // returned from generateStaticParams diff --git a/test/e2e/app-dir/app-static/app-static.test.ts b/test/e2e/app-dir/app-static/app-static.test.ts index a42d5a6df6551..d91935a7be360 100644 --- a/test/e2e/app-dir/app-static/app-static.test.ts +++ b/test/e2e/app-dir/app-static/app-static.test.ts @@ -43,12 +43,8 @@ createNextDescribe( 'blog/tim.rsc', 'blog/tim/first-post.html', 'blog/tim/first-post.rsc', - 'dynamic-error/[id].html', - 'dynamic-error/[id].rsc', 'dynamic-error/[id]/page.js', 'dynamic-no-gen-params-ssr/[slug]/page.js', - 'dynamic-no-gen-params/[slug].html', - 'dynamic-no-gen-params/[slug].rsc', 'dynamic-no-gen-params/[slug]/page.js', 'force-dynamic-no-prerender/[id]/page.js', 'force-static/[slug]/page.js', @@ -79,11 +75,7 @@ createNextDescribe( 'ssr-auto/cache-no-store/page.js', 'ssr-auto/fetch-revalidate-zero/page.js', 'ssr-forced/page.js', - 'static-to-dynamic-error-forced/[id].html', - 'static-to-dynamic-error-forced/[id].rsc', 'static-to-dynamic-error-forced/[id]/page.js', - 'static-to-dynamic-error/[id].html', - 'static-to-dynamic-error/[id].rsc', 'static-to-dynamic-error/[id]/page.js', 'variable-revalidate-edge/no-store/page.js', 'variable-revalidate-edge/revalidate-3/page.js', From b854804f32a2f77698747dc13c27948b667d0d7f Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Wed, 22 Feb 2023 22:23:10 +0100 Subject: [PATCH 2/3] fix prerendering --- packages/next/src/build/index.ts | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index 7773c6f70593b..e01644fc36263 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -1435,22 +1435,11 @@ export default async function build( isSsg = true } - const isDynamic = isDynamicRoute(page) - const isDynamicPageWithoutGenerateStaticParams = - isDynamic && !workerResult.prerenderRoutes - if (workerResult.appConfig?.revalidate !== 0) { - if (!isDynamic) { + if (!isDynamicRoute(page)) { appStaticPaths.set(originalAppPath, [page]) appStaticPathsEncoded.set(originalAppPath, [page]) isStatic = true - } else if (isDynamicPageWithoutGenerateStaticParams) { - // Do not prerender with the empty slug name if there - // is no generateStaticParams provided. - // https://github.com/vercel/next.js/issues/45850 - appStaticPaths.set(originalAppPath, []) - appStaticPathsEncoded.set(originalAppPath, []) - isStatic = true } } From 2b3158f480dcfb010726c82e6eb6830698751423 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Wed, 22 Feb 2023 23:16:20 +0100 Subject: [PATCH 3/3] fix --- packages/next/src/build/index.ts | 27 +++++++++++--- .../e2e/app-dir/app-static/app-static.test.ts | 35 ------------------- 2 files changed, 22 insertions(+), 40 deletions(-) diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index e01644fc36263..f897fed7dc12f 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -1435,11 +1435,31 @@ export default async function build( isSsg = true } + const appConfig = workerResult.appConfig || {} if (workerResult.appConfig?.revalidate !== 0) { - if (!isDynamicRoute(page)) { + const isDynamic = isDynamicRoute(page) + const hasGenerateStaticParams = + !!workerResult.prerenderRoutes?.length + + if ( + // Mark the app as static if: + // - It has no dynamic param + // - It doesn't have generateStaticParams but `dynamic` is set to + // `error` or `force-static` + !isDynamic + ) { appStaticPaths.set(originalAppPath, [page]) appStaticPathsEncoded.set(originalAppPath, [page]) isStatic = true + } else if ( + isDynamic && + !hasGenerateStaticParams && + (appConfig.dynamic === 'error' || + appConfig.dynamic === 'force-static') + ) { + appStaticPaths.set(originalAppPath, []) + appStaticPathsEncoded.set(originalAppPath, []) + isStatic = true } } @@ -1448,10 +1468,7 @@ export default async function build( // returned from generateStaticParams appDynamicParamPaths.add(originalAppPath) } - appDefaultConfigs.set( - originalAppPath, - workerResult.appConfig || {} - ) + appDefaultConfigs.set(originalAppPath, appConfig) } } else { if (isEdgeRuntime(pageRuntime)) { diff --git a/test/e2e/app-dir/app-static/app-static.test.ts b/test/e2e/app-dir/app-static/app-static.test.ts index d91935a7be360..e71e73363fbce 100644 --- a/test/e2e/app-dir/app-static/app-static.test.ts +++ b/test/e2e/app-dir/app-static/app-static.test.ts @@ -209,16 +209,6 @@ createNextDescribe( fallback: null, routeRegex: '^\\/dynamic\\-error\\/([^\\/]+?)(?:\\/)?$', }, - '/dynamic-no-gen-params/[slug]': { - dataRoute: '/dynamic-no-gen-params/[slug].rsc', - dataRouteRegex: normalizeRegEx( - '^\\/dynamic\\-no\\-gen\\-params\\/([^\\/]+?)\\.rsc$' - ), - fallback: null, - routeRegex: normalizeRegEx( - '^\\/dynamic\\-no\\-gen\\-params\\/([^\\/]+?)(?:\\/)?$' - ), - }, '/hooks/use-pathname/[slug]': { dataRoute: '/hooks/use-pathname/[slug].rsc', dataRouteRegex: normalizeRegEx( @@ -259,16 +249,6 @@ createNextDescribe( '^\\/static\\-to\\-dynamic\\-error\\-forced\\/([^\\/]+?)(?:\\/)?$' ), }, - '/static-to-dynamic-error/[id]': { - dataRoute: '/static-to-dynamic-error/[id].rsc', - dataRouteRegex: normalizeRegEx( - '^\\/static\\-to\\-dynamic\\-error\\/([^\\/]+?)\\.rsc$' - ), - fallback: null, - routeRegex: normalizeRegEx( - '^\\/static\\-to\\-dynamic\\-error\\/([^\\/]+?)(?:\\/)?$' - ), - }, }) }) @@ -316,21 +296,6 @@ createNextDescribe( expect($2('#page-data').text()).not.toBe(pageData) }) } else { - it('should properly error when static page switches to dynamic at runtime', async () => { - const res = await next.fetch( - '/static-to-dynamic-error/static-bailout-1' - ) - - expect(res.status).toBe(500) - - if (isNextStart) { - await check( - () => stripAnsi(next.cliOutput), - /Page changed from static to dynamic at runtime \/static-to-dynamic-error\/static-bailout-1, reason: cookies/ - ) - } - }) - it('should not error with dynamic server usage with force-static', async () => { const res = await next.fetch( '/static-to-dynamic-error-forced/static-bailout-1'