Unable to Set Cookies from Express.js to Next.js SSR Component (app router) #77116
Replies: 1 comment 1 reply
-
I believe you're running into an issue because SSR requests in Next.js don’t automatically persist cookies like a browser does. When you fetch from Express in an SSR component, even if Express sends the Set-Cookie header, it won’t be applied to future SSR requests. Basically client side requests work because the browser handles cookies automatically. I think you can fix it by manually set cookies in SSR:
other then that you can also try:
Instead of calling Express from the SSR component, call a Next.js API route. I believe this will solve the issue :) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have an Express.js backend running on http://localhost:8000 and a Next.js frontend (App Router) running on http://localhost:3000.
What Works: • When making a request from a Next.js client component (browser) to the Express backend, cookies are set successfully. • When checking the response headers in a server component (SSR) or API route, I can see the Set-Cookie header coming from Express.
What Works: • When making a request from a Next.js client component (browser) to the Express backend, cookies are set successfully. • When checking the response headers in a server component (SSR) or API route, I can see the Set-Cookie header coming from Express.
What I Tried: • Setting credentials: "include" in the fetch request. • Making a Next.js API route as a proxy and forwarding the Set-Cookie header. • Using different SameSite policies (Lax, Strict, None with Secure). • Ensuring CORS is properly configured on Express (Access-Control-Allow-Credentials: true). • Manually setting a cookie in the Next.js response (nextResponse.cookies.set("key", "value")).
This is my SSR function on NextJS to fetch request on ExpressJS API. To get info if user is authenticated or not I am sending access token. If access token is expired and refresh token is valid then I am refreshing the access token but new access token is not adding to browser cookies.
this is my expressjs refreshToken function where i setting new accessToken but it not working as i say above. it only works when request comes from nextjs client component but not when request comes from nextjs ssr
Beta Was this translation helpful? Give feedback.
All reactions