New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redesign the cookies API for NextResponse #31719
Labels
Middleware
Related to Next.js Middleware
Milestone
Comments
javivelasco
changed the title
NextResponse: better cookies API
NextResponse: redesign cookies API
Dec 21, 2021
javivelasco
changed the title
NextResponse: redesign cookies API
Redesign the cookies API for NextResponse
Dec 22, 2021
Would this redesign extend to |
kodiakhq bot
pushed a commit
that referenced
this issue
May 9, 2022
This PR introduces a more predictable API to manipulate cookies in an Edge Function context. ```js const response = new NextResponse() // set a cookie response.cookies.set('foo, 'bar') // => set-cookie: 'foo=bar; Path=/'` // set another cookie response.cookies.set('fooz, 'barz') // => set-cookie: 'foo=bar; Path=/, fooz=barz; Path=/'` // delete a cookie means mark it as expired response.cookies.delete('foo') // => set-cookie: 'foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT, fooz=barz; Path=/'` // clear all cookies means mark all of them as expired response.cookies.clear() // => set-cookie: 'fooz=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT, foo=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT'` ``` This new cookies API uses [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) interface, and it's available for `NextRequest` and `NextResponse`. Additionally, you can pass a specific cookies option as a third argument in `set` method: ```js response.cookies.set('foo', 'bar', { path: '/', maxAge: 60 * 60 * 24 * 7, httpOnly: true, sameSite: 'strict', domain: 'example.com' } ``` **Note**: `maxAge` it's in seconds rather than milliseconds. Any cookie manipulation will be reflected over the `set-cookie` header, transparently. closes #31719
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. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Right now
NextResponse
is exposing some public methods for handling cookies:Although they are doing the job, they feel like hard to know they exist or what they do outside the class.
Describe the solution you'd like
I think cookies methods can be accommodated in a better way.
My proposal is to interact with cookies in a very similar way to a Map instance.
So rather than have some public methods,
NextResponse
just has acookie
instance with all cookies methods related living as instance methods, such as:Also, this way expose a better surface for adding more methods in the future, like:
Describe alternatives you've considered
Internally, it could be implemented using a map instance and just exposing the methods we want to expose.
The text was updated successfully, but these errors were encountered: