NextRequest
Source URL: https://nextjs.org/docs/pages/api-reference/functions/next-request
NextRequest
Section titled “NextRequest”NextRequest extends the Web Request API with additional convenience methods.
cookies
Section titled “cookies”Read or mutate the Set-Cookie header of the request.
set(name, value)
Section titled “set(name, value)”Given a name, set a cookie with the given value on the request.
// Given incoming request /home// Set a cookie to hide the banner// request will have a `Set-Cookie:show-banner=false;path=/home` headerrequest.cookies.set('show-banner', 'false')get(name)
Section titled “get(name)”Given a cookie name, return the value of the cookie. If the cookie is not found, undefined is returned. If multiple cookies are found, the first one is returned.
// Given incoming request /home// { name: 'show-banner', value: 'false', Path: '/home' }request.cookies.get('show-banner')getAll()
Section titled “getAll()”Given a cookie name, return the values of the cookie. If no name is given, return all cookies on the request.
// Given incoming request /home// [// { name: 'experiments', value: 'new-pricing-page', Path: '/home' },// { name: 'experiments', value: 'winter-launch', Path: '/home' },// ]request.cookies.getAll('experiments')// Alternatively, get all cookies for the requestrequest.cookies.getAll()delete(name)
Section titled “delete(name)”Given a cookie name, delete the cookie from the request.
// Returns true for deleted, false is nothing is deletedrequest.cookies.delete('experiments')has(name)
Section titled “has(name)”Given a cookie name, return true if the cookie exists on the request.
// Returns true if cookie exists, false if it does notrequest.cookies.has('experiments')clear()
Section titled “clear()”Remove all cookies from the request.
request.cookies.clear()nextUrl
Section titled “nextUrl”Extends the native URL API with additional convenience methods, including Next.js specific properties.
// Given a request to /home, pathname is /homerequest.nextUrl.pathname// Given a request to /home?name=lee, searchParams is { 'name': 'lee' }request.nextUrl.searchParamsThe following options are available:
| Property | Type | Description |
|---|---|---|
basePath | string | The base path of the URL. |
buildId | string | undefined |
defaultLocale | string | undefined |
domainLocale | ||
- defaultLocale | string | The default locale within a domain. |
- domain | string | The domain associated with a specific locale. |
- http | boolean | undefined |
locales | string[] | undefined |
locale | string | undefined |
url | URL | The URL object. |
Version History
Section titled “Version History”| Version | Changes |
|---|---|
v15.0.0 | ip and geo removed. |