Update Cookie inside server Component.
Answered
Banjara Hound posted this in #help-forum
Banjara HoundOP
i have a server action method that get data from server and return response.
inside server action i check that token valid or not. if is not, update token. but when token is not valid i can't set cookie because i get exception that cookie can only be modified inside API Route or Server Actions.
i know how to fix this with using useTransition. but i want a way to use it inside serve component. not inside client component. and i know i can use startTransition directly but can't use async/await method inside that. and can't return data inside startTransition .
inside server action i check that token valid or not. if is not, update token. but when token is not valid i can't set cookie because i get exception that cookie can only be modified inside API Route or Server Actions.
i know how to fix this with using useTransition. but i want a way to use it inside serve component. not inside client component. and i know i can use startTransition directly but can't use async/await method inside that. and can't return data inside startTransition .
22 Replies
@Banjara Hound i have a server action method that get data from server and return response.
inside server action i check that token valid or not. if is not, update token. but when token is not valid i can't set cookie because i get exception that cookie can only be modified inside API Route or Server Actions.
i know how to fix this with using useTransition. but i want a way to use it inside serve component. not inside client component. and i know i can use startTransition directly but can't use async/await method inside that. and can't return data inside startTransition .
if you get the error
A server action is a request made on the browser to the route after the page rendered.
cookie can only be modified inside API Route or Server Actions. which mean you are executing the function in a server component.A server action is a request made on the browser to the route after the page rendered.
currently, we can't set the cookies in the page response
https://github.com/vercel/next.js/discussions/58110
https://github.com/vercel/next.js/discussions/58110
Banjara HoundOP
so what should i do? i want user get data when page rendered.
since that data depend on valid token.
you can read the cookie on the page but not set
Banjara HoundOP
so how to update cookie when it's not valid?
you will have to set the cookie with server action if it is not valid
or do it in middleware
Banjara HoundOP
if i do it in middleware, is not gonna slow down? because i do it now and it's a little slow.
I think the speed should have a little different only if you are doing in on middleware or page?
Banjara HoundOP
my api handle auth and i just want a way to store cookie in browser and update it when it's not valid. i do that with httpOnly cookie. is there any better way or any custom lib that can do that without any problem?
Pollock
Hi
Inside your server action file make sure you put 'use server' at the top. You need to explicitly type 'use server' at the top for it to be a server action otherwise it'll default to a server component - which won't allow you to set a cookie.
You can inline 'use server' but for simplicty sake it'll make it easier
Inside your server action file make sure you put 'use server' at the top. You need to explicitly type 'use server' at the top for it to be a server action otherwise it'll default to a server component - which won't allow you to set a cookie.
You can inline 'use server' but for simplicty sake it'll make it easier
@Pollock Hi
Inside your server action file make sure you put 'use server' at the top. You need to explicitly type 'use server' at the top for it to be a server action otherwise it'll default to a server component - which won't allow you to set a cookie.
You can inline 'use server' but for simplicty sake it'll make it easier
Banjara HoundOP
at top of file has use server and i have other actions that work find when i submit form.
I would do that on middleware
Banjara HoundOP
ok thanks. i will do it with middleware until i find better way to do that.
or may be someone has better way.
currently only middleware
for this case
Answer
hope this will change
Banjara HoundOP
yes i hope that nextjs provide better way for this kind of situation.
thanks for helping
thanks for helping
yeah I think it will since its still new