From 1c89da60f0f13b57c1f37c32dd9bd6ccee77990e Mon Sep 17 00:00:00 2001 From: Bruno Nascimento <34252418+brvnonascimento@users.noreply.github.com> Date: Thu, 1 Dec 2022 01:28:05 -0300 Subject: [PATCH] fix: Dynamic Usage Error when using previewData with generateStaticParams and appDir (#43395) fixes #43392 ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [] Related issues linked using `fixes #number` - [] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [x] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) Co-authored-by: JJ Kasper --- packages/next/client/components/headers.ts | 4 -- test/e2e/app-dir/app-static.test.ts | 38 +++++++++++++++++++ .../app/ssg-preview/[[...route]]/page.js | 29 ++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 test/e2e/app-dir/app-static/app/ssg-preview/[[...route]]/page.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-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-static/app/ssg-preview/[[...route]]/page.js b/test/e2e/app-dir/app-static/app/ssg-preview/[[...route]]/page.js new file mode 100644 index 000000000000..5b606dbfbab3 --- /dev/null +++ b/test/e2e/app-dir/app-static/app/ssg-preview/[[...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 +}