Closed KolmschateS closed 1 year ago
Have a look at this example repo and see if that helps @KolmschateS .. On second thought, you're trying to use that cookie setting function in the render and the error tells you why. Error: Cookies can only be modified in a Server Action or Route Handler.
@johnson-jesse True, I got it working with a form, but I am not trying to work with a form here. So I'm curious if there is a use case or workaround possible to use cookies outside of a form. Maybe I have to use the useEffect(), I'll have a look when I have time.
It will still work as a custom invocation server action or as a use server function passed into a component accessed with useEffect
@KolmschateS I think it's safe to close this issue
using useEffect and useState to fetch the server action worked. Thanks.
We can't set the maxAge or other headers in server actions. We need to be able to do it according to the document.
@soylemezali42 I got my code to work. This is a seperate issue I think, maybe you can open one?
@KolmschateS . Thank you for notification. I already have opened a new issue.
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.
Verify canary release
Provide environment information
Which area(s) of Next.js are affected? (leave empty if unsure)
No response
Link to the code that reproduces this issue
https://github.com/KolmschateS/next-set-cookies-reproduction
To Reproduce
create this
Vists.tsx
, a Component fileenable server actions in
next.config.js
file to enable server actions with the following code in the nextconfig jsonimplement it in a page by using importing < Vistis /> and the error should appear when loading the page the components is in.
switch between the main branch and 13-4-2-bug branch to see the different between canary(main) and the 13.4.2 version.
Describe the Bug
I am trying to set a get and set a cookie at the same time to monitor the amount of times a user has visited a page, has to be done for a school assignment. It has to be stored in a cookie.
Next.js 13.4.2 gives me an error that I cannot use cookies.set(), which is described as working in the docs:
Which should be fixed in 13.4.2 according to this issue. But after updating to 13.4.2 I still get the same error.
Updating to canary gives me another error 18-05-2023 12:00 ET:
Which is what I'm doing, if I'm correct.
I'm new to Next, does anybody know a fix or what I'm doing wrong?
Expected Behavior
I expect to be able to set the cookie using the server action.
Which browser are you using? (if relevant)
Firefox Developer edition 114.0b5 (64-bit)
How are you deploying your application? (if relevant)
No response