Edge Runtime Cookies
The @edge-runtime/cookies package provides high-level HTTP Cookie abstractions for Edge-compatible environments, such as Edge Runtime or Vercel Edge Functions and Edge Middleware.
The available methods are based on the CookieStore API.
The main difference is that the methods are not asynchronous so they do not return a Promise.
Installation
npm install @edge-runtime/cookiesUsage
for Request
To access and manipulate request cookies, use the exported RequestCookies constructor:
import { RequestCookies } from '@edge-runtime/cookies'
function handleRequest(req: Request) {
const cookies = new RequestCookies(req.headers)
cookies.get('cookie-name')?.value // undefined | string
cookies.has('cookie-name') // boolean
// do something...
}Notes:
-
All mutations are performed in place and will update the
Cookieheader in the providedRequestobject. -
When mutating the request cookies, the client won’t be able to read the updated/deleted cookies. Please use
ResponseCookiesfor that.
Available methods
get- A method that takes a cookienameand returns an object withnameandvalue. If a cookie withnameisn’t found, it returnsundefined. If multiple cookies match, it will only return the first match. The cookie configuration (Max-Age, Path etc) is not being passed by the HTTP client, therefore it’s not possible to determine the cookie expiration date.getAll- A method that is similar toget, but returns a list of all the cookies with a matchingname. Ifnameis unspecified, it returns all the available cookies.set- A method that takes an object with properties ofCookieListItemas defined in the W3C CookieStore API spec.delete- A method that takes either a cookienameor a list of names. and removes the cookies matching the name(s). Returnstruefor deleted andfalsefor undeleted cookies.has- A method that takes a cookienameand returns abooleanbased on if the cookie exists (true) or not (false).clear- A method that takes no argument and will effectively remove theCookieheader.
for Response
To access and manipulate response cookies that will persist in the HTTP client,
use the exported ResponseCookies constructor:
import { ResponseCookies } from '@edge-runtime/cookies'
function handleRequest(req: Request) {
const cookieKey = 'cookie-name'
const headers = new Headers()
const responseCookies = new ResponseCookies(headers)
cookies.set('cookie-name', 'cookie-value', { maxAge: 1000 }) // make cookie persistent for 1000 seconds
cookies.delete('old-cookie')
return new Response(null, {
headers,
status: 200,
})
}Available methods
get- A method that takes a cookienameand returns an object withnameandvalue. If a cookie withnameisn’t found, it returnsundefined. If multiple cookies match, it will only return the first match.getAll- A method that is similar toget, but returns a list of all the cookies with a matchingname. Ifnameis unspecified, it returns all the available cookies.set- A method that takes an object with properties ofCookieListItemas defined in the W3C CookieStore API spec.delete- A method that takes either a cookienameor a list of names. and removes the cookies matching the name(s). Returnstruefor deleted andfalsefor undeleted cookies.