Skip to content
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

Closed
Kikobeats opened this issue Nov 23, 2021 · 2 comments · Fixed by #36478
Closed

Redesign the cookies API for NextResponse #31719

Kikobeats opened this issue Nov 23, 2021 · 2 comments · Fixed by #36478
Labels
Middleware Related to Next.js Middleware

Comments

@Kikobeats
Copy link
Member

Kikobeats commented Nov 23, 2021

Right now NextResponse is exposing some public methods for handling cookies:

  • clearCookie
  • cookie
  • 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 a cookie instance with all cookies methods related living as instance methods, such as:

  • cookie.set (instead of cookie)
  • cookie.delete (instead of clearCookie)
  • cookie.getAll (instead of cookies)

Also, this way expose a better surface for adding more methods in the future, like:

  • cookie.get
  • cookie.clear
  • cookie.has
  • etc

Describe alternatives you've considered

Internally, it could be implemented using a map instance and just exposing the methods we want to expose.

@javivelasco javivelasco changed the title NextResponse: better cookies API NextResponse: redesign cookies API Dec 21, 2021
@javivelasco javivelasco added the Middleware Related to Next.js Middleware label Dec 22, 2021
@javivelasco javivelasco changed the title NextResponse: redesign cookies API Redesign the cookies API for NextResponse Dec 22, 2021
@javivelasco javivelasco added this to the Next.js Middleware GA milestone Jan 20, 2022
@anthonyalayo
Copy link

Would this redesign extend to NextApiResponse as well? For example, having your API clear the cookie on /auth/logout?

@kodiakhq kodiakhq bot closed this as completed in #36478 May 9, 2022
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
@github-actions
Copy link
Contributor

github-actions bot commented Jun 8, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Middleware Related to Next.js Middleware
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants