How to set cookies from server components via route handlers? #72957
Unanswered
danieljcksn
asked this question in
Help
Replies: 1 comment
-
I am new to Next as well and running into the same problem. I have a page that renders both a server component and a client component. Both of my components need to update the cookies. If I call the route handler from a client component, the cookies are set and visible on the browser, however, when calling the route handler from a sever component, it gets lost in the middle. I am not sure if this is the right way to do it or not, but would really appreciate any help here. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey Next.js devs, I've run into an issue and wanted to see if anyone else has insight or experience with this.
The Situation:
Set-Cookie
header doesn't get sent to the client. However, if I make the same request directly from the browser (e.g., typing the URL in the address bar or usingfetch
in a client component), the cookie is set as expected.Set-Cookie
header from being part of the response that the browser receives.Has Anyone Else Run Into This?
Is this expected behavior, or am I missing something? If this is normal, how should cookies be managed when requests are made from Server Components? Are there any best practices or workarounds for handling HTTP-only cookies, reading them, and ensuring they can be updated properly in these cases?
Steps to Reproduce:
Route Handler (
app/api/set-cookie/route.ts
)Server Component (
app/page.tsx
)What's Happening:
When I open this server component, the cookie isn't set. But if I access the route handler directly (e.g.,
http://localhost:3000/api/set-cookie
) or use a client-side request, the cookie gets set as expected.I'd love to hear if anyone has experience with this or knows of a good solution. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions