Next.js Discord

Discord Forum

Server component making an API request to change cookie values

Answered
American posted this in #help-forum
Open in Discord
AmericanOP
When a server component makes an API request to an endpoint that sets cookies, whether it deletes cookies or sets new cookie values. Why am I not seeing the changes when I go to the Application tab and then the Cookies section?

I'm trying to learn access and refresh token rotation by implementing the system myself. However, it seems like Server components can't directly set new cookie values. Middleware don't trigger every time because of some sort of caching. And now route handlers invoked by Server components can't set new cookie values without having to convert the component to a client component and trigger router.refresh()?
Answered by fuma
Server components are rendered on server, so setting a cookie to request sent from server components won’t affect anything on client.
Hence, you must fire it on client. Mark your component with “use client” and fire it somewhere.
View full answer

4 Replies

Server components are rendered on server, so setting a cookie to request sent from server components won’t affect anything on client.
Hence, you must fire it on client. Mark your component with “use client” and fire it somewhere.
Answer
AmericanOP
Then where should the logic of token rotation be placed? Middleware does not always trigger which could be problematic so...
Generally, in a useEffect
I would do token rotation in middleware instead