// pages/_middleware.tsimport { NextResponse } from'next/server'importtype { NextRequest } from'next/server'exportfunctionmiddleware(request:NextRequest) {// create an instance of the class to access the public methods. This uses `next()`,// you could use `redirect()` or `rewrite()` as welllet response =NextResponse.next()// get the cookies from the requestlet cookieFromRequest =request.cookies['my-cookie']// set the `cookie`response.cookie('hello','world')// set the `cookie` with optionsconstcookieWithOptions=response.cookie('hello','world', { path:'/', maxAge:1000*60*60*24*7, httpOnly:true, sameSite:'strict', domain:'example.com', })// clear the `cookie`response.clearCookie('hello')return response}
exportfunctionmiddleware() {constresponse=newNextResponse()// set a cookieresponse.cookies.set('vercel','fast')// set another cookie with optionsresponse.cookies.set('nextjs','awesome', { path:'/test' })// get all the details of a cookieconst { value,...options } =response.cookies.getWithOptions('vercel')console.log(value) // => 'fast'console.log(options) // => { name: 'vercel', Path: '/test' }// deleting a cookie will mark it as expiredresponse.cookies.delete('vercel')return response}