matthewmueller / next-cookies

Tiny little function for getting cookies on both client & server with next.js.
368 stars 17 forks source link

Issue with reading a cookie #47

Closed hellozuech closed 2 years ago

hellozuech commented 3 years ago

When using NextCookie there appears to be a bug.

Issue with Reading cookies in ^2.0.3 on NPM

downgrade to ^1.1.3 works

Issue reading Req when attempting to read a cookie

nfriedly commented 3 years ago

I'm not sure.. that's not really enough to pinpoint anything specific.

I will mention that 1.x uses a completely different underlying library than 2.x, so it's not very surprising that there will be differences in behavior.

Gentatsu commented 2 years ago

Get this issue as well.

image

nfriedly commented 2 years ago

I'd need to see your code to know for sure, but that stack trace indicates that next-cookies is being called directly by react without any of your code in-between - are you trying to use next-cookies as a high-order component? Because that won't work.

The call to next-cookies has to be inside of the getInitialProps method (or getStaticProps or getServerSideProps) so that you can pass in the context variable. Example.

Gentatsu commented 2 years ago

I'm indeed not passing in the context. This was the case prior to upgrading to 2.0.3, and it worked fine. I've migrated the codebase to use js-cookie instead, and this seems to work fine inside HOCs.

nfriedly commented 2 years ago

Ok, that was your issue. Passing in context was always a required part of the API, but in 1.x the client code path didn't actually use it, so incorrect usage didn't always result in errors. In 2.x it's required for either client or server usage, so failing to provide it will always result in an error.

In either case, it should work inside of a HOC, it just isn't a HOC itself.

(I mostly just wanted to clarify those two points in case anyone else came along with a similar issue. If you're happy with js-cookie, then stick with that :)