Replies: 1 comment 6 replies
-
You can’t set cookies directly from Server Components in Next.js 15.5 because of React’s streaming — the response headers are already sent before cookies().set() runs. |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I’m new to Next.js and I’m having trouble setting cookies in version 15.5. My app uses a separate backend that returns a JWT token for authentication and has a refresh endpoint that issues a new token when the old one expires. I need to set and update cookies entirely on the server side, but it doesn’t seem to work unless I use Server Actions or Route Handlers.
Additional information
In my setup, there is no login form handled by Next.js - authentication happens through an external backend. After receiving the JWT, I want to store it as a cookie using Next.js server code.
However, when I try to set the cookie server-side, it never actually gets set. I suspect this happens because of the new streaming architecture in Next.js 15.5, where responses start streaming before headers (like cookies) can be modified.
I’ve read the documentation, but I haven’t found a clear way to handle this kind of flow without using a Route Handler or Server Action.
What’s the correct way to set or update cookies on the server side in this scenario?
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions