Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next.js Static Generation Issue with Layout Cookies and Dynamic Routes #1605

Open
3 tasks done
LoganBaek97 opened this issue Dec 12, 2024 · 3 comments
Open
3 tasks done
Labels
bug Something isn't working unconfirmed Needs triage.

Comments

@LoganBaek97
Copy link

LoganBaek97 commented Dec 12, 2024

Description

When a higher-level layout uses cookies and a nested dynamic route page ([locale]/test/[code]/page.tsx) with an empty generateStaticParams() is accessed, a DYNAMIC_SERVER_USAGE error occurs. Interestingly, accessing [locale]/page.tsx does not trigger this error.

Detailed Observations

Accessing cookies in the top-level layout (src/app/layout.tsx)
Scenario 1: Page with Empty generateStaticParams()

  • Error: DYNAMIC_SERVER_USAGE
  • Occurs when accessing src/app/[locale]/test/[code]/page.tsx

Scenario 2: Root Page

  • No error
  • Accessing src/[locale]/page.tsx works normally

Attempted Solutions

  1. Returning a non-empty array in generateStaticParams()
  2. Using export const dynamic = 'force-static' - Enables successful static build

Build Log Details

When running pnpm build -d, the build log shows:

  • "Static generation failed due to dynamic usage on /en, reason: cookies", ...
  • Applies to all static pages

Questions

  • What causes the DYNAMIC_SERVER_USAGE error in nested dynamic routes?
  • Why does force-static resolve the build issue?
  • Why do build logs indicate dynamic usage due to cookies even after using force-static?

Verifications

Mandatory reproduction URL

https://github.com/LoganBaek97/next-intl/tree/main/examples/example-app-router

Reproduction description

Steps to reproduce:

  1. Open reproduction
  2. Clone project
  3. See src/app/[locale]/test/[code]/page.tsx, src/app/[locale].tsx and src/app/layout.tsx (using cookies)

Expected behaviour

  • Successful static generation for all pages
  • No dynamic usage warnings when using layout-level cookies
@LoganBaek97 LoganBaek97 added bug Something isn't working unconfirmed Needs triage. labels Dec 12, 2024
@amannn
Copy link
Owner

amannn commented Dec 13, 2024

Hmm, for me it seems to work in your repro:

Screenshot 2024-12-13 at 11 20 51

Strangely even if I move cookies() to app/[locale]/layout.tsx. Not sure if there's a bug here in Next.js.

If I remove generateStaticParams in app/[locale]/layout.tsx, then the result looks more like what I'd expect:

Screenshot 2024-12-13 at 11 23 13

This is somewhat related: vercel/next.js#64616. I hope that rootParams brings some improvements here, at least the case of a static layout + dynamic child is covered in the tests in the PR.

@LoganBaek97
Copy link
Author

@amannn
During the build log, the page appears to be generated statically. However, when running pnpm build -d (to view detailed build logs), an error message is displayed: "Static generation failed due to dynamic usage on /en (and other pages too) , reason: cookies". By rendering new Date().toISOString() on the pages where static generation failed, we can confirm that the static build is indeed unsuccessful (the time changes with each page refresh and is not fixed). I expected this might be a Next.js issue and hope it can be resolved quickly. Thank you.

@daveycodez
Copy link

daveycodez commented Dec 17, 2024

Error occurred prerendering page "/en". Read more: https://nextjs.org/docs/messages/prerender-error
Error: Route /[locale] with dynamic = "error" couldn't be rendered statically because it used headers. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering

Builds as static just fine until I try to use output: "export" which is, a requirement for Capacitor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

3 participants