vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.86k stars 26.86k forks source link

Server Component: Dynamic server usage: cookies when using generateStaticParams with next/headers #49066

Open Metarock opened 1 year ago

Metarock commented 1 year ago

Verify canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #202303130630~1681329778~22.04~d824cd4 SMP PREEMPT_DYNAMIC Wed A
    Binaries:
      Node: 18.15.0
      npm: 9.5.0
      Yarn: 1.22.19
      pnpm: 7.29.0
    Relevant packages:
      next: 13.3.4
      eslint-config-next: 13.3.4
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue

Unable to provide

To Reproduce

  1. Follow install docs for nextjs supabase: https://supabase.com/docs/guides/auth/auth-helpers/nextjs-server-components
  2. add a generateStaticParams

Describe the Bug

Using generateStaticParams with supabase is causing an Unhandled Runtime Error Error: Dynamic server usage: cookies

image

Expected Behavior

Able to use generateStaticParams with supabase

Which browser are you using? (if relevant)

Firefox 112.0.2(64-bit)

How are you deploying your application? (if relevant)

Vercel

RyelBanfield commented 1 year ago

export const revalidate = 0;

Adding this on my layout worked for me. I think it's fine?

Edit: This changes all routes to be SSR including any landing pages. So use it either directly on the pages with the issues or on a nested layout.

Jordaneisenburger commented 1 year ago

Just a beginners thought: I believe this is not an issue with Next.js as generateStaticParams tells NextJS to statically generate the page. But it's detecting that you're using a cookie somewhere in your code making it actually a dynamic page.

elie222 commented 1 month ago

Ran into this too when trying to use Posthog on the server, which sets cookies for the id.

It makes sense that the static pages shouldn't support this, but there should still be an easy way to call cookies in a layout, that doesn't impact static pages. Solution might be to use multiple layouts.