diff --git a/test/production/app-dir/generate-static-params-errors/app/[lang]/connection/[slug]/page.tsx b/test/production/app-dir/generate-static-params-errors/app/[lang]/connection/[slug]/page.tsx new file mode 100644 index 00000000000000..f9344b736dcae1 --- /dev/null +++ b/test/production/app-dir/generate-static-params-errors/app/[lang]/connection/[slug]/page.tsx @@ -0,0 +1,10 @@ +import { connection } from 'next/server' + +export default function Page() { + return
connection
+} + +export async function generateStaticParams() { + await connection() + return [{ slug: 'test' }] +} diff --git a/test/production/app-dir/generate-static-params-errors/app/[lang]/cookies/[slug]/page.tsx b/test/production/app-dir/generate-static-params-errors/app/[lang]/cookies/[slug]/page.tsx new file mode 100644 index 00000000000000..a2dae649d3de61 --- /dev/null +++ b/test/production/app-dir/generate-static-params-errors/app/[lang]/cookies/[slug]/page.tsx @@ -0,0 +1,10 @@ +import { cookies } from 'next/headers' + +export default function Page() { + returncookies
+} + +export async function generateStaticParams() { + await cookies() + return [{ slug: 'test' }] +} diff --git a/test/production/app-dir/generate-static-params-errors/app/[lang]/draft-mode/[slug]/page.tsx b/test/production/app-dir/generate-static-params-errors/app/[lang]/draft-mode/[slug]/page.tsx new file mode 100644 index 00000000000000..5149b6b905016a --- /dev/null +++ b/test/production/app-dir/generate-static-params-errors/app/[lang]/draft-mode/[slug]/page.tsx @@ -0,0 +1,10 @@ +import { draftMode } from 'next/headers' + +export default function Page() { + returndraft-mode
+} + +export async function generateStaticParams() { + await draftMode() + return [{ slug: 'test' }] +} diff --git a/test/production/app-dir/generate-static-params-errors/app/[lang]/headers/[slug]/page.tsx b/test/production/app-dir/generate-static-params-errors/app/[lang]/headers/[slug]/page.tsx new file mode 100644 index 00000000000000..72556e06218b1e --- /dev/null +++ b/test/production/app-dir/generate-static-params-errors/app/[lang]/headers/[slug]/page.tsx @@ -0,0 +1,10 @@ +import { headers } from 'next/headers' + +export default function Page() { + returnheaders
+} + +export async function generateStaticParams() { + await headers() + return [{ slug: 'test' }] +} diff --git a/test/production/app-dir/generate-static-params-errors/app/[lang]/layout.tsx b/test/production/app-dir/generate-static-params-errors/app/[lang]/layout.tsx new file mode 100644 index 00000000000000..ab4ac77ac18526 --- /dev/null +++ b/test/production/app-dir/generate-static-params-errors/app/[lang]/layout.tsx @@ -0,0 +1,11 @@ +export default function Root({ children }: { children: React.ReactNode }) { + return ( + + {children} + + ) +} + +export async function generateStaticParams() { + return [{ lang: 'en' }] +} diff --git a/test/production/app-dir/generate-static-params-errors/app/[lang]/root-params/[slug]/page.tsx b/test/production/app-dir/generate-static-params-errors/app/[lang]/root-params/[slug]/page.tsx new file mode 100644 index 00000000000000..ff54989d92b3a8 --- /dev/null +++ b/test/production/app-dir/generate-static-params-errors/app/[lang]/root-params/[slug]/page.tsx @@ -0,0 +1,10 @@ +import { lang } from 'next/root-params' + +export default function Page() { + returnroot-params
+} + +export async function generateStaticParams() { + await lang() + return [{ slug: 'test' }] +} diff --git a/test/production/app-dir/generate-static-params-errors/generate-static-params-errors.test.ts b/test/production/app-dir/generate-static-params-errors/generate-static-params-errors.test.ts new file mode 100644 index 00000000000000..a9994c7df46ec5 --- /dev/null +++ b/test/production/app-dir/generate-static-params-errors/generate-static-params-errors.test.ts @@ -0,0 +1,56 @@ +import { nextTestSetup } from 'e2e-utils' + +describe('generate-static-params-errors', () => { + const { next } = nextTestSetup({ + files: __dirname, + skipStart: true, + }) + + let cliOutputLength: number + + afterEach(async () => { + await next.stop() + }) + + const buildRoute = async (routePath: string) => { + cliOutputLength = next.cliOutput.length + await next.build({ args: ['--debug-build-paths', routePath] }) + } + + const getCliOutput = () => next.cliOutput.slice(cliOutputLength) + + it('should error when cookies() is called inside generateStaticParams', async () => { + await buildRoute('app/[lang]/cookies/[slug]/page.tsx') + expect(getCliOutput()).toContain( + 'Error: `cookies` was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context' + ) + }) + + it('should error when headers() is called inside generateStaticParams', async () => { + await buildRoute('app/[lang]/headers/[slug]/page.tsx') + expect(getCliOutput()).toContain( + 'Error: `headers` was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context' + ) + }) + + it('should error when connection() is called inside generateStaticParams', async () => { + await buildRoute('app/[lang]/connection/[slug]/page.tsx') + expect(getCliOutput()).toContain( + 'Error: `connection` was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context' + ) + }) + + it('should error when draftMode() is called inside generateStaticParams', async () => { + await buildRoute('app/[lang]/draft-mode/[slug]/page.tsx') + expect(getCliOutput()).toContain( + 'Error: `draftMode` was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context' + ) + }) + + it('should error when root params are accessed inside generateStaticParams', async () => { + await buildRoute('app/[lang]/root-params/[slug]/page.tsx') + expect(getCliOutput()).toContain( + "Error: Route /[lang]/root-params/[slug] used `import('next/root-params').lang()` outside of a Server Component. This is not allowed." + ) + }) +}) diff --git a/test/production/app-dir/generate-static-params-errors/next.config.ts b/test/production/app-dir/generate-static-params-errors/next.config.ts new file mode 100644 index 00000000000000..42500be934ae56 --- /dev/null +++ b/test/production/app-dir/generate-static-params-errors/next.config.ts @@ -0,0 +1,7 @@ +import type { NextConfig } from 'next' + +const nextConfig: NextConfig = { + experimental: { rootParams: true }, +} + +export default nextConfig