nookies icon indicating copy to clipboard operation
nookies copied to clipboard

Cookies = {} server side

Open denny7 opened this issue 2 years ago • 2 comments

I'm setting a cookie server side. When I check the browser, the cookie is set properly. But when I set the cookie and immediately after that try to get it, it is undefined.

example code:

setCookie(isServer ? ctx : {}, 'testCookie', 'cookie value', {path: '/'});
const testCookie = parseCookies(isServer ? ctx : {}).testCookie;
console.log(testCookie);

The code above logs me undefined. This is happen in getInitialProps in next.js. When I try it client side (same code in ComponentDidMout), everything is working properly. Is nookies are async when are used server side? How to read the cookie, immediately after set it on server side?

denny7 avatar Nov 24 '22 16:11 denny7

you should probably provide path: '/' with the options when reading the cookie:

parseCookies(isServer ? ctx : {}, { path: '/' }).testCookie

viniciusdacal avatar Jan 01 '23 17:01 viniciusdacal

you should probably provide path: '/' with the options when reading the cookie:

parseCookies(isServer ? ctx : {}, { path: '/' }).testCookie

It doesn't work

denny7 avatar May 23 '23 13:05 denny7