From d97a66c467ef97277a5a125bc1066da665b89b2f Mon Sep 17 00:00:00 2001 From: Bruno Nascimento Date: Fri, 25 Nov 2022 16:08:31 -0300 Subject: [PATCH 1/3] fix: Dynamic Usage Error when using previewData with generateStaticParams and appDir --- packages/next/client/components/headers.ts | 4 --- test/e2e/app-dir/app-preview-static.test.ts | 30 +++++++++++++++++++ .../app/[[...route]]/page.js | 29 ++++++++++++++++++ .../app/layout.js | 5 ++-- .../app-dir/app-preview-static/next.config.js | 7 +++++ 5 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 test/e2e/app-dir/app-preview-static.test.ts create mode 100644 test/e2e/app-dir/app-preview-static/app/[[...route]]/page.js rename test/e2e/app-dir/{app-static => app-preview-static}/app/layout.js (51%) create mode 100644 test/e2e/app-dir/app-preview-static/next.config.js diff --git a/packages/next/client/components/headers.ts b/packages/next/client/components/headers.ts index 42dc2741f97e..19fcf902fbb7 100644 --- a/packages/next/client/components/headers.ts +++ b/packages/next/client/components/headers.ts @@ -16,10 +16,6 @@ export function headers() { } export function previewData() { - if (staticGenerationBailout('previewData')) { - return {} - } - const requestStore = requestAsyncStorage && 'getStore' in requestAsyncStorage ? requestAsyncStorage.getStore()! diff --git a/test/e2e/app-dir/app-preview-static.test.ts b/test/e2e/app-dir/app-preview-static.test.ts new file mode 100644 index 000000000000..f155b1ef7642 --- /dev/null +++ b/test/e2e/app-dir/app-preview-static.test.ts @@ -0,0 +1,30 @@ +import { NextInstance } from 'test/lib/next-modes/base' +import { createNext, FileRef } from 'e2e-utils' +import path from 'path' +import webdriver from 'next-webdriver' + +describe('app-dir previewData with SSG', () => { + if ((global as any).isNextDeploy) { + it('should skip next deploy for now', () => {}) + return + } + + let next: NextInstance + + afterAll(() => next.destroy()) + beforeAll(async () => { + next = await createNext({ + files: new FileRef(path.join(__dirname, 'app-preview-static')), + }) + }) + + it('Should not throw Dynamic Server Usage error when using generateStaticParams with previewData', async () => { + const browserOnIndexPage = await webdriver(next.url, '/') + + const content = await browserOnIndexPage + .elementByCss('#preview-data') + .text() + + expect(content).toContain('previewData') + }) +}) diff --git a/test/e2e/app-dir/app-preview-static/app/[[...route]]/page.js b/test/e2e/app-dir/app-preview-static/app/[[...route]]/page.js new file mode 100644 index 000000000000..2e373b4873d7 --- /dev/null +++ b/test/e2e/app-dir/app-preview-static/app/[[...route]]/page.js @@ -0,0 +1,29 @@ +import { previewData } from 'next/headers' + +export default function Page() { + const previewDataResult = previewData() + + return ( +
+
+        {JSON.stringify({ previewData: previewDataResult })}
+      
+
+ ) +} + +export const generateStaticParams = async () => { + const paths = [ + { + route: [''], + }, + { + route: ['test'], + }, + { + route: ['test-2'], + }, + ] + + return paths +} diff --git a/test/e2e/app-dir/app-static/app/layout.js b/test/e2e/app-dir/app-preview-static/app/layout.js similarity index 51% rename from test/e2e/app-dir/app-static/app/layout.js rename to test/e2e/app-dir/app-preview-static/app/layout.js index f37ea744b8e0..7d00201b8ce2 100644 --- a/test/e2e/app-dir/app-static/app/layout.js +++ b/test/e2e/app-dir/app-preview-static/app/layout.js @@ -1,9 +1,10 @@ -export default function Layout({ children }) { +export default function RootLayout({ children }) { return ( - my static blog + static with previewData + {children} ) diff --git a/test/e2e/app-dir/app-preview-static/next.config.js b/test/e2e/app-dir/app-preview-static/next.config.js new file mode 100644 index 000000000000..492259bc8076 --- /dev/null +++ b/test/e2e/app-dir/app-preview-static/next.config.js @@ -0,0 +1,7 @@ +const nextConfig = { + experimental: { + appDir: true, + }, +} + +module.exports = nextConfig From 0075452f0d55b59eb829cb3a75e94935ca579cce Mon Sep 17 00:00:00 2001 From: Bruno Nascimento Date: Fri, 25 Nov 2022 19:51:20 -0300 Subject: [PATCH 2/3] fix: missing root layout on app-static --- test/e2e/app-dir/app-static/app/layout.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/e2e/app-dir/app-static/app/layout.js diff --git a/test/e2e/app-dir/app-static/app/layout.js b/test/e2e/app-dir/app-static/app/layout.js new file mode 100644 index 000000000000..f37ea744b8e0 --- /dev/null +++ b/test/e2e/app-dir/app-static/app/layout.js @@ -0,0 +1,10 @@ +export default function Layout({ children }) { + return ( + + + my static blog + + {children} + + ) +} From ab39b983b534361b50f8f17698d3eff9db0d947d Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 30 Nov 2022 19:39:26 -0800 Subject: [PATCH 3/3] Update test --- test/e2e/app-dir/app-preview-static.test.ts | 30 --------------- .../app-dir/app-preview-static/app/layout.js | 11 ------ .../app-dir/app-preview-static/next.config.js | 7 ---- test/e2e/app-dir/app-static.test.ts | 38 +++++++++++++++++++ .../app/ssg-preview}/[[...route]]/page.js | 2 +- 5 files changed, 39 insertions(+), 49 deletions(-) delete mode 100644 test/e2e/app-dir/app-preview-static.test.ts delete mode 100644 test/e2e/app-dir/app-preview-static/app/layout.js delete mode 100644 test/e2e/app-dir/app-preview-static/next.config.js rename test/e2e/app-dir/{app-preview-static/app => app-static/app/ssg-preview}/[[...route]]/page.js (95%) diff --git a/test/e2e/app-dir/app-preview-static.test.ts b/test/e2e/app-dir/app-preview-static.test.ts deleted file mode 100644 index f155b1ef7642..000000000000 --- a/test/e2e/app-dir/app-preview-static.test.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { NextInstance } from 'test/lib/next-modes/base' -import { createNext, FileRef } from 'e2e-utils' -import path from 'path' -import webdriver from 'next-webdriver' - -describe('app-dir previewData with SSG', () => { - if ((global as any).isNextDeploy) { - it('should skip next deploy for now', () => {}) - return - } - - let next: NextInstance - - afterAll(() => next.destroy()) - beforeAll(async () => { - next = await createNext({ - files: new FileRef(path.join(__dirname, 'app-preview-static')), - }) - }) - - it('Should not throw Dynamic Server Usage error when using generateStaticParams with previewData', async () => { - const browserOnIndexPage = await webdriver(next.url, '/') - - const content = await browserOnIndexPage - .elementByCss('#preview-data') - .text() - - expect(content).toContain('previewData') - }) -}) diff --git a/test/e2e/app-dir/app-preview-static/app/layout.js b/test/e2e/app-dir/app-preview-static/app/layout.js deleted file mode 100644 index 7d00201b8ce2..000000000000 --- a/test/e2e/app-dir/app-preview-static/app/layout.js +++ /dev/null @@ -1,11 +0,0 @@ -export default function RootLayout({ children }) { - return ( - - - static with previewData - - - {children} - - ) -} diff --git a/test/e2e/app-dir/app-preview-static/next.config.js b/test/e2e/app-dir/app-preview-static/next.config.js deleted file mode 100644 index 492259bc8076..000000000000 --- a/test/e2e/app-dir/app-preview-static/next.config.js +++ /dev/null @@ -1,7 +0,0 @@ -const nextConfig = { - experimental: { - appDir: true, - }, -} - -module.exports = nextConfig diff --git a/test/e2e/app-dir/app-static.test.ts b/test/e2e/app-dir/app-static.test.ts index 4fb6309d9066..c3f3f3a66eaf 100644 --- a/test/e2e/app-dir/app-static.test.ts +++ b/test/e2e/app-dir/app-static.test.ts @@ -66,6 +66,13 @@ describe('app-dir static/dynamic handling', () => { 'hooks/use-pathname/slug.html', 'hooks/use-pathname/slug.rsc', 'hooks/use-search-params/[slug]/page.js', + 'ssg-preview.html', + 'ssg-preview.rsc', + 'ssg-preview/[[...route]]/page.js', + 'ssg-preview/test-2.html', + 'ssg-preview/test-2.rsc', + 'ssg-preview/test.html', + 'ssg-preview/test.rsc', 'ssr-auto/cache-no-store/page.js', 'ssr-auto/fetch-revalidate-zero/page.js', 'ssr-forced/page.js', @@ -145,6 +152,21 @@ describe('app-dir static/dynamic handling', () => { initialRevalidateSeconds: false, srcRoute: '/force-static/[slug]', }, + '/ssg-preview': { + dataRoute: '/ssg-preview.rsc', + initialRevalidateSeconds: false, + srcRoute: '/ssg-preview/[[...route]]', + }, + '/ssg-preview/test': { + dataRoute: '/ssg-preview/test.rsc', + initialRevalidateSeconds: false, + srcRoute: '/ssg-preview/[[...route]]', + }, + '/ssg-preview/test-2': { + dataRoute: '/ssg-preview/test-2.rsc', + initialRevalidateSeconds: false, + srcRoute: '/ssg-preview/[[...route]]', + }, }) expect(manifest.dynamicRoutes).toEqual({ '/blog/[author]/[slug]': { @@ -171,10 +193,26 @@ describe('app-dir static/dynamic handling', () => { fallback: null, routeRegex: '^\\/force\\-static\\/([^\\/]+?)(?:\\/)?$', }, + '/ssg-preview/[[...route]]': { + dataRoute: '/ssg-preview/[[...route]].rsc', + dataRouteRegex: '^\\/ssg\\-preview(?:\\/(.+?))?\\.rsc$', + fallback: null, + routeRegex: '^\\/ssg\\-preview(?:\\/(.+?))?(?:\\/)?$', + }, }) }) } + it('Should not throw Dynamic Server Usage error when using generateStaticParams with previewData', async () => { + const browserOnIndexPage = await webdriver(next.url, '/ssg-preview') + + const content = await browserOnIndexPage + .elementByCss('#preview-data') + .text() + + expect(content).toContain('previewData') + }) + it('should force SSR correctly for headers usage', async () => { const res = await fetchViaHTTP(next.url, '/force-static', undefined, { headers: { diff --git a/test/e2e/app-dir/app-preview-static/app/[[...route]]/page.js b/test/e2e/app-dir/app-static/app/ssg-preview/[[...route]]/page.js similarity index 95% rename from test/e2e/app-dir/app-preview-static/app/[[...route]]/page.js rename to test/e2e/app-dir/app-static/app/ssg-preview/[[...route]]/page.js index 2e373b4873d7..5b606dbfbab3 100644 --- a/test/e2e/app-dir/app-preview-static/app/[[...route]]/page.js +++ b/test/e2e/app-dir/app-static/app/ssg-preview/[[...route]]/page.js @@ -15,7 +15,7 @@ export default function Page() { export const generateStaticParams = async () => { const paths = [ { - route: [''], + route: [], }, { route: ['test'],