moshest / next-client-cookies

SSR and client support for cookies in Next.js 13 (app directory)
MIT License
144 stars 9 forks source link

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

Closed mordechaim closed 1 week ago

mordechaim commented 1 month ago

The following code: https://github.com/moshest/next-client-cookies/blob/68439a612ab339307ff833cadd2ac8ff6c730ed5/src/server.tsx#L9

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 commented 1 month ago

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

moshest commented 1 month ago

Released under 2.0.0-alpha.0.

Can you check?

mordechaim commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

This warning is gone now, works perfectly

moshest commented 1 month ago

Great!

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

RobinGiel commented 1 week ago

Thanks for the fix, Next 15 is now stable

moshest commented 1 week ago

Available now under v2.0.0