matthewmueller / next-cookies

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

Cannot read property 'headers' of undefined #15

Closed himadrinath closed 5 years ago

himadrinath commented 5 years ago

@matthewmueller eveything working fine until i update to nextjs 8.0.3 (next-cookies 1.1.1)

same code working before but now getting this on the client side

Error in error page getInitialProps: TypeError: Cannot read property 'headers' of undefined

if (!ctx.req.headers) return {}; // for Static export feature of Next.js

pointing this line in next-cookies.node.js

nfriedly commented 5 years ago

Is the error happening in the browser or in the node js side? Also, is it when you're just running the site regularly, or is it during a static export?

himadrinath commented 5 years ago

@nfriedly in browser side. just running the site during routing

nfriedly commented 5 years ago

Can you try with next-cookies@1.1.0 and @1.0.4, and let me know if it works with either of those versions?

npm i next-cookies@1.1.0 --save-exact npm i next-cookies@1.0.4 --save-exact

himadrinath commented 5 years ago

@nfriedly ok

himadrinath commented 5 years ago

@nfriedly 1.1.0 working. not tested 1.0.4

nfriedly commented 5 years ago

Okay, cool, that's a good short-term solution. I'll try and figure out what happened with 1.1.1.

himadrinath commented 5 years ago

Thanks

matthewmueller commented 5 years ago

Sorry about that, we really need to get some tests going for this repo.

I'm a little confused though because it seems like the culprit is on the server-side:

https://github.com/matthewmueller/next-cookies/blob/master/next-cookies.node.js#L18

Can you check if this works?

if (!ctx.req || !ctx.req.headers) return {}

EDIT: Is the !ctx.req.headers conditional in your getInitialProps code?

nfriedly commented 5 years ago

I think something is causing the client to run the server code.

I agree about needing tests. I used nightwatch once before and I think it might be part of the answer here.

nfriedly commented 5 years ago

Here's the issue: #14 moved the browser code to the dist/ folder, but also added it to .gitignore. Because there's no .npmignore file, npm defaults to ignoring the items listed in the .gitignore file. So, the minified browser code didn't get published to npm.

Without the specified browser code available, webpack is falling back to the server code which, of course, doesn't work in the browser.

nfriedly commented 5 years ago

I just published a v1.1.2 that should address this. @himadrinath can you test it out and confirm?

npm i next-cookies@1.1.2 --save

himadrinath commented 5 years ago

ok

himadrinath commented 5 years ago

@nfriedly its working. thanks

nfriedly commented 5 years ago

Perfect! Thank you for confirming, and sorry for the trouble.

andrew-oko-odion commented 5 years ago

Hey this is still happening I can't read cookies in getInitialProps

jasonlor commented 5 years ago

Still happening on my end

yrabinov commented 5 years ago

Happening for me as well

Clement-Bresson commented 5 years ago

For me too

matthewmueller commented 5 years ago

@nfriedly any ideas?

Clement-Bresson commented 5 years ago

In my case, problem was I tried to use it in a HOC wraping around next _app.js...

I have not digged how next works internally, but at this stage it seems ctx is not the same, and ctx.req is undefined.

Using last version of next-cookies in deeper components/HOC (like using it on any normal page in pages folder) works like a charm.