Files
Delba de OliveiraandGitHub 2eaaad2d16 Docs: Share Redirecting docs with /pages (#61083)
- Share new **Redirecting** docs with /pages
- Add equivalent example for API routes in /pages
2024-01-24 08:23:38 -06:00

152 lines
7.3 KiB
Plaintext

---
title: NextRequest
description: API Reference for NextRequest.
---
{/* The content of this doc is shared between the app and pages router. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}
NextRequest extends the [Web Request API](https://developer.mozilla.org/docs/Web/API/Request) with additional convenience methods.
## `cookies`
Read or mutate the [`Set-Cookie`](https://developer.mozilla.org/docs/Web/HTTP/Headers/Set-Cookie) header of the request.
### `set(name, value)`
Given a name, set a cookie with the given value on the request.
```ts
// Given incoming request /home
// Set a cookie to hide the banner
// request will have a `Set-Cookie:show-banner=false;path=/home` header
request.cookies.set('show-banner', 'false')
```
### `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.
```ts
// Given incoming request /home
// { name: 'show-banner', value: 'false', Path: '/home' }
request.cookies.get('show-banner')
```
### `getAll()`
Given a cookie name, return the values of the cookie. If no name is given, return all cookies on the request.
```ts
// 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 request
request.cookies.getAll()
```
### `delete(name)`
Given a cookie name, delete the cookie from the request.
```ts
// Returns true for deleted, false is nothing is deleted
request.cookies.delete('experiments')
```
### `has(name)`
Given a cookie name, return `true` if the cookie exists on the request.
```ts
// Returns true if cookie exists, false if it does not
request.cookies.has('experiments')
```
### `clear()`
Remove the `Set-Cookie` header from the request.
```ts
request.cookies.clear()
```
## `nextUrl`
Extends the native [`URL`](https://developer.mozilla.org/docs/Web/API/URL) API with additional convenience methods, including Next.js specific properties.
```ts
// Given a request to /home, pathname is /home
request.nextUrl.pathname
// Given a request to /home?name=lee, searchParams is { 'name': 'lee' }
request.nextUrl.searchParams
```
The following options are available:
<PagesOnly>
| Property | Type | Description |
| ----------------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `basePath` | `string` | The [base path](/docs/pages/api-reference/next-config-js/basePath) of the URL. |
| `buildId` | `string` \| `undefined` | The build identifier of the Next.js application. Can be [customized](/docs/pages/api-reference/next-config-js/generateBuildId). |
| `defaultLocale` | `string` \| `undefined` | The default locale for [internationalization](/docs/pages/building-your-application/routing/internationalization). |
| `domainLocale` | | |
| - `defaultLocale` | `string` | The default locale within a domain. |
| - `domain` | `string` | The domain associated with a specific locale. |
| - `http` | `boolean` \| `undefined` | Indicates if the domain is using HTTP. |
| `locales` | `string[]` \| `undefined` | An array of available locales. |
| `locale` | `string` \| `undefined` | The currently active locale. |
| `url` | `URL` | The URL object. |
</PagesOnly>
<AppOnly>
| Property | Type | Description |
| -------------- | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `basePath` | `string` | The [base path](/docs/app/api-reference/next-config-js/basePath) of the URL. |
| `buildId` | `string` \| `undefined` | The build identifier of the Next.js application. Can be [customized](/docs/app/api-reference/next-config-js/generateBuildId). |
| `pathname` | `string` | The pathname of the URL. |
| `searchParams` | `Object` | The search parameters of the URL. |
> **Note:** The internationalization properties from the Pages Router are not available for usage in the App Router. Learn more about [internationalization with the App Router](/docs/app/building-your-application/routing/internationalization).
</AppOnly>
## `ip`
The `ip` property is a string that contains the IP address of the request. This value can optionally be provided by your hosting platform.
> **Good to know:** On [Vercel](https://vercel.com/docs/frameworks/nextjs?utm_source=next-site&utm_medium=docs&utm_campaign=next-website), this value is provided by default. On other platforms, you can use the [`X-Forwarded-For`](https://developer.mozilla.org/docs/Web/HTTP/Headers/X-Forwarded-For) header to provide the IP address.
```ts
// Provided by Vercel
request.ip
// Self-hosting
request.headers.get('X-Forwarded-For')
```
## `geo`
The `geo` property is an object that contains the geographic information of the request. This value can optionally be provided by your hosting platform.
> **Good to know:** On [Vercel](https://vercel.com/docs/frameworks/nextjs?utm_source=next-site&utm_medium=docs&utm_campaign=next-website), this value is provided by default. On other platforms, you can use the [`X-Forwarded-For`](https://developer.mozilla.org/docs/Web/HTTP/Headers/X-Forwarded-For) header to provide the IP address, then use a [third-party service](https://ip-api.com/) to lookup the geographic information.
```ts
// Provided by Vercel
request.geo.city
request.geo.country
request.geo.region
request.geo.latitude
request.geo.longitude
// Self-hosting
function getGeo(request) {
let ip = request.headers.get('X-Forwarded-For')
// Use a third-party service to lookup the geographic information
}
```