Error: Cookies can only be modified in a Server Action or Route Handler.
Unanswered
Sloth bear posted this in #help-forum
Sloth bearOP
Hello. Does anyone know what's wrong?
26 Replies
Sloth bearOP
Is there any way to set cookies in action functions that do not execute actions in component (like a form submit)? Only get data from the API and save it
in middleware
Yellowstripe scad
You can set cookies in route handlers too
@Yellowstripe scad You can set cookies in route handlers too
Sloth bearOP
Yeah but only when I trigger anything, no?
@Ray in middleware
Sloth bearOP
I tried now but gives the same error
using .set() in middleware inside
@Sloth bear I tried now but gives the same error
use
req.cookie.set() in middleware@Ray use `req.cookie.set()` in middleware
Sloth bearOP
Oh , sure. I can access that cookie in client side?
Using a hook for example
@Sloth bear Oh , sure. I can access that cookie in client side?
no if its a http only cookie
whyd you need that? next-auth provided a hook for fetching session data in client
or you could implement the hook yourself
instead of store the session data in a cookie which have a limited 4k size
@Ray no if its a http only cookie
Sloth bearOP
I need cache an api result
I want to make a cache of the result to prevent requests extras in the same timd
Time
Yellowstripe scad
Ok, I use unstable_cache from next/cache for similar use cases, example, I need the plan information of the current user in several server components... working well so far
I will see
Thx ray and tomaspozo
@Yellowstripe scad Ok, I use unstable_cache from next/cache for similar use cases, example, I need the plan information of the current user in several server components... working well so far
Sloth bearOP
I can revalidate this cache when I want? Or only setting a seconds
in function
Sloth bearOP
as "revalidateTag()"
@Sloth bear I can revalidate this cache when I want? Or only setting a seconds
yes you can revalidate it with the tag
@Sloth bear Hello. Does anyone know what's wrong?
weird bug. whats the error log?