next.js icon indicating copy to clipboard operation
next.js copied to clipboard

[NEXT-695] Unable to use `cookies()` in layout component when rendering a page that exports generateStaticParams

Open JarrodMFlesch opened this issue 2 years ago • 3 comments

Verify canary release

  • [X] I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System: Platform: linux Arch: x64 Version: #22 SMP Tue Jan 10 18:39:00 UTC 2023 Binaries: Node: 16.17.0 npm: 8.15.0 Yarn: 1.22.19 pnpm: 7.1.0 Relevant packages: next: 13.1.7-canary.15 eslint-config-next: 13.1.2 react: 18.2.0 react-dom: 18.2.0

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

App directory (appDir: true)

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/floral-platform-owohud?file=%2FREADME.md&selection=%5B%7B%22endColumn%22%3A17%2C%22endLineNumber%22%3A7%2C%22startColumn%22%3A17%2C%22startLineNumber%22%3A7%7D%5D

To Reproduce

Click on either link when the app boots up.

Describe the Bug

When using generateStaticParams, you cannot use import { cookies } from "next/headers"; in any layout component.

Expected Behavior

Use import { cookies } from "next/headers"; in any layout component and load the dynamic route page.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-695

JarrodMFlesch avatar Feb 16 '23 02:02 JarrodMFlesch

@JarrodMFlesch I'm currently having the issue that I can't use cookie() in my layout.tsx. Do you know of any workaround for how to get an auth token from the cookie into a fetch request inside generateStaticParams?

renekahr avatar Aug 06 '23 10:08 renekahr

@JarrodMFlesch I'm having the same issue. Did you find a solution?

nicvazquez avatar Sep 27 '23 17:09 nicvazquez

Ran into the same issue myself, could not find a proper solution. I get a 500 internal server error which only happened in production environments.

I ended up creating a separate layout file for the static routes and foregoing using the cookies for now.

staticpo avatar Jan 30 '24 20:01 staticpo