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 v15-canary: access to cookies() should be asynchronously #22

Open
mordechaim opened this issue Sep 30, 2024 · 6 comments · May be fixed by #23
Open

Next.js v15-canary: access to cookies() should be asynchronously #22

mordechaim opened this issue Sep 30, 2024 · 6 comments · May be fixed by #23

Comments

@mordechaim
Copy link

The following code:

const secretValue = cookies().getAll();

Triggers a warning on next@canary:

In route / a cookie property was accessed directly with `cookies().getAll()`. `cookies()` now returns a Promise and the return value should be awaited before accessing properties of the underlying cookies instance. In this version of Next.js direct access to `cookies().getAll()` is still supported to facilitate migration but in a future version you will be required to await the result. If this `cookies()` use is inside an async function await the return value before accessing attempting iteration. If this use is inside a synchronous function then convert the function to async or await the call from outside this function and pass the result in.

Rational

@mordechaim
Copy link
Author

The fix should be simple, since we are in a server component, mark the function with async and call the function with await

@moshest moshest linked a pull request Oct 1, 2024 that will close this issue
@moshest
Copy link
Owner

moshest commented Oct 1, 2024

Released under 2.0.0-alpha.0.

Can you check?

@mordechaim
Copy link
Author

No warnings now, useCookies() works perfectly.

Although I never use it in my app, I did test getCookies() as well (what's the purpose, we can just use cookies()?). It now properly returns a promise, but Typescript doesn't detect it as such:

'await' has no effect on the type of this expression

@moshest
Copy link
Owner

moshest commented Oct 1, 2024

Thank you. You're right. I've update the types on version 2.0.0-alpha.1.

The getCookies() is just a wrapper, but it provide the same interface as you have on the client side so it's useful if you need to share cookies logic.

@mordechaim
Copy link
Author

This warning is gone now, works perfectly

@moshest
Copy link
Owner

moshest commented Oct 1, 2024

Great!

I will keep this issue open until Next.js canary is officially released and we can release it as well.

@moshest moshest changed the title Next.js now requires access to cookies() to be called asynchronously Next.js v15: access to cookies() should be asynchronously Oct 1, 2024
@moshest moshest changed the title Next.js v15: access to cookies() should be asynchronously Next.js v15-canary: access to cookies() should be asynchronously Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants