matthewmueller / next-cookies

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

prevent require('cookie') from ending up on the client-side #11

Closed matthewmueller closed 5 years ago

matthewmueller commented 5 years ago

Didn't test but while looking at the source again I realized that the server-side require('cookie') is most likely ending up on the client-side.

I think we should maybe break the cookie parser out into another file that webpack/next will conditionally load. I think webpack respects the browser field so maybe we could use that.

wdifruscio commented 5 years ago

Hi There,

Pretty sure this is related to this issue, Our production nextjs project was breaking on iOS 9.3 and we could not figure out why for a really long time. Took another shot at it recently and found that the culprit was this package. I copied the source code into my project manually and it ended up working fine

tomwayson commented 5 years ago

Looks like there are a few ways to do this:

https://arunoda.me/blog/ssr-and-server-only-modules

nfriedly commented 5 years ago

This is now fixed in v1.1.0. Apologies for the delay.

FWIW, the linked post above is aimed more at library consumers rather than authors. The method I chose, which makes more sense for a library author, is to split the browser and node.js code into separate files, and then use the main and browser fields in package.json to identify these files. Node.js loads the main file, Webpack will automatically load the browser file when bundling for a browser.

https://docs.npmjs.com/files/package.json#main

(This is essentially what @matthewmueller suggested)

matthewmueller commented 5 years ago

awesome, thanks @nfriedly!