Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
124 lines
6.1 KiB
Plaintext
124 lines
6.1 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 all cookies 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/config/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/config/next-config-js/generateBuildId). |
|
|
| `defaultLocale` | `string` \| `undefined` | The default locale for [internationalization](/docs/pages/guides/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/config/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/config/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/guides/internationalization).
|
|
|
|
</AppOnly>
|
|
|
|
## Version History
|
|
|
|
| Version | Changes |
|
|
| --------- | ----------------------- |
|
|
| `v15.0.0` | `ip` and `geo` removed. |
|