Session ID Changes on Every Request: Next.js with Express.js Backend #59230
Replies: 2 comments
-
Hi, Let's be careful with names here, middleware runs on an special environment called, edge runtime. It is not a React Server Component, though I guess technically, since it is part of a Next.js application, and it doesn't run in the client, one might call it a Server Component, but that's really stretching concepts. Middleware is used for other purposes, and it uses different technologies. That being said, what is happening is that you are making an assumption when you fly the fetch request within the middleware, and that is you are assuming that the middleware will forward its headers to the caller. This applies even for Node.js fetch requests, you are essentially, starting a new request from a new environment, ask yourself: what origin header should be used for a fetch request started from within a Node.js script? You'll need to forward the headers you care about, taking them from the |
Beta Was this translation helpful? Give feedback.
-
I'm also having the same problem could you find the solution |
Beta Was this translation helpful? Give feedback.
-
I'm experiencing an issue with session IDs changing on every request in a Next.js server component (middleware.ts). I'm using Next.js v14 for the frontend and Express.js for the backend. The session IDs are obtained from an Express.js backend using the express-session middleware. Here's an overview of my setup:
In my middleware.ts, I'm fetching the login status from the Express.js backend, but I'm getting different session IDs on each request. From the client component, the session works perfectly. I suspect that the origin may be changing from the server component on every request.
Middleware OP:
middleware.ts
responses were like followingExpress.js session:
From the front-end(client component) session is working perfectly. But, when I tried with server component whether it is
middleware.ts
or next.js/api
on every request I am getting differentsessionID
I think the origin get changed from server component on every request!I want to know why the session ID is changing on every request when using a server component.
Beta Was this translation helpful? Give feedback.
All reactions