Cookies which are set from server are missing in the first load: [email protected] #24722
Replies: 4 comments 1 reply
-
Are you sure they are loaded by the second time and are not reminiscent of the previous load? Did you pay attention to those two caveats before deciding to handle cookies in _app.js: Adding a custom getInitialProps in your App will disable Automatic Static Optimization in pages without Static Generation. When you add getInitialProps in your custom app, you must import App from "next/app", call App.getInitialProps(appContext) inside getInitialProps and merge the returned object into the return value. Usually, I try to use _app.js only for what is necessary. Handling cookies can be done with some layout wrap on every page and I don't have to use getInitialProps. |
Beta Was this translation helpful? Give feedback.
-
I understand. Still hard for me to come up with a good theory. We know that this will always be false at first -> isWindow() getInitialProps does not run like useEffect(). Did you investigate when you have true conditions? Maybe it's only when you're leaving a page in a fast refresh. |
Beta Was this translation helpful? Give feedback.
-
this problem still persists in app router version 15.2.4 nextjs. |
Beta Was this translation helpful? Give feedback.
-
You're running into a common issue when setting cookies via a Cloudflare Worker and trying to read them server-side on the first page load (using getInitialProps in Next.js). Here's a breakdown of what's going on and how you might fix or work around it. Root Cause: Cloudflare Workers set cookies via response headers, but: On the very first request, if the cookie was just set by the Worker (using Set-Cookie header), that cookie is not yet available in the request headers that Next.js receives (i.e., ctx.req.headers.cookie). Only subsequent requests or a hard reload will include those cookies in the request headers, since the browser persists them after receiving the response. Solutions / Workarounds
Middleware runs before rendering and can read and modify cookies. You can use request.cookies from NextRequest.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am reading the cookies which are set from cloudflare worker in _app.js. getInitialProps
I am facing a issue that on the first load of the page the cookies set from cloudflare worker are not coming. If i do hard refresh of the page then cookies are loading.
Can someone help me, that how can i resolve this.
Beta Was this translation helpful? Give feedback.
All reactions