-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading cookies in _document.js (nextjs) works on localhost but not in production. #201
Comments
Hey, Cookies are a client-only thing, can you try setting the Adding
|
Thank you very much for the quick response but can you explain in more detail. I'm new to web dev.🙈 I try to read the cookies in order to conditionally run scripts in _document.js' element - doesn't _document.js run on server? |
Sure, There's 2 reasons:
Why not make it server-side? well.... Some cookies, like we use in the cookiebanner, are strictly a client-side thing so it's impossible to detect this particular type of cookie on the server and pre-render the cookiebanner. Think of it like this:
If we were to render it on the server side then we'd need to know something about the user to verify who they are, so one could argue we are storing sensitive data (or at least identifiable data, probably even before login). Lots of extra things to take care off, whilst rendering this one particular component on the client-side doesn't incur much cost at all. |
On localhost it just works perfect. This is my cookies component and how i set cookies using react-cookies-consent:
And this is how I read them (nookies.js) after user makes choice and inits a refresh of the page:
Hosting: Vercel
Libs:
"next": "^14.0.4",
"react-cookie-consent": "^9.0.0",
"nookies": "^2.5.2",
The text was updated successfully, but these errors were encountered: