mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
Based on feedback from https://github.com/vercel/next.js/issues/47793, I made some improvements around the geolocation docs. Specifically around `request.ip`, `request.geo`, and how to access these values. I noticed there was a bit of a divergence, as some of the `NextRequest` and `NextResponse` docs were split out for the App Router section, but not all. This PR finishes that swing by removing the previous catch-all for `next/server` in the Pages Router docs and splits them into individual docs pages. Wrote a lil' thread about this: https://twitter.com/leeerob/status/1736543599339172121 --------- Co-authored-by: Delba de Oliveira <[email protected]>
147 lines
4.2 KiB
Plaintext
147 lines
4.2 KiB
Plaintext
---
|
|
title: NextResponse
|
|
description: API Reference for NextResponse.
|
|
---
|
|
|
|
{/* 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. */}
|
|
|
|
NextResponse extends the [Web Response API](https://developer.mozilla.org/docs/Web/API/Response) with additional convenience methods.
|
|
|
|
## `cookies`
|
|
|
|
Read or mutate the [`Set-Cookie`](https://developer.mozilla.org/docs/Web/HTTP/Headers/Set-Cookie) header of the response.
|
|
|
|
### `set(name, value)`
|
|
|
|
Given a name, set a cookie with the given value on the response.
|
|
|
|
```ts
|
|
// Given incoming request /home
|
|
let response = NextResponse.next()
|
|
// Set a cookie to hide the banner
|
|
response.cookies.set('show-banner', 'false')
|
|
// Response will have a `Set-Cookie:show-banner=false;path=/home` header
|
|
return response
|
|
```
|
|
|
|
### `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
|
|
let response = NextResponse.next()
|
|
// { name: 'show-banner', value: 'false', Path: '/home' }
|
|
response.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 response.
|
|
|
|
```ts
|
|
// Given incoming request /home
|
|
let response = NextResponse.next()
|
|
// [
|
|
// { name: 'experiments', value: 'new-pricing-page', Path: '/home' },
|
|
// { name: 'experiments', value: 'winter-launch', Path: '/home' },
|
|
// ]
|
|
response.cookies.getAll('experiments')
|
|
// Alternatively, get all cookies for the response
|
|
response.cookies.getAll()
|
|
```
|
|
|
|
### `delete(name)`
|
|
|
|
Given a cookie name, delete the cookie from the response.
|
|
|
|
```ts
|
|
// Given incoming request /home
|
|
let response = NextResponse.next()
|
|
// Returns true for deleted, false is nothing is deleted
|
|
response.cookies.delete('experiments')
|
|
```
|
|
|
|
## `json()`
|
|
|
|
Produce a response with the given JSON body.
|
|
|
|
```ts filename="app/api/route.ts" switcher
|
|
import { NextResponse } from 'next/server'
|
|
|
|
export async function GET(request: Request) {
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
|
|
}
|
|
```
|
|
|
|
```js filename="app/api/route.js" switcher
|
|
import { NextResponse } from 'next/server'
|
|
|
|
export async function GET(request) {
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
|
|
}
|
|
```
|
|
|
|
## `redirect()`
|
|
|
|
Produce a response that redirects to a [URL](https://developer.mozilla.org/docs/Web/API/URL).
|
|
|
|
```ts
|
|
import { NextResponse } from 'next/server'
|
|
|
|
return NextResponse.redirect(new URL('/new', request.url))
|
|
```
|
|
|
|
The [URL](https://developer.mozilla.org/docs/Web/API/URL) can be created and modified before being used in the `NextResponse.redirect()` method. For example, you can use the `request.nextUrl` property to get the current URL, and then modify it to redirect to a different URL.
|
|
|
|
```ts
|
|
import { NextResponse } from 'next/server'
|
|
|
|
// Given an incoming request...
|
|
const loginUrl = new URL('/login', request.url)
|
|
// Add ?from=/incoming-url to the /login URL
|
|
loginUrl.searchParams.set('from', request.nextUrl.pathname)
|
|
// And redirect to the new URL
|
|
return NextResponse.redirect(loginUrl)
|
|
```
|
|
|
|
## `rewrite()`
|
|
|
|
Produce a response that rewrites (proxies) the given [URL](https://developer.mozilla.org/docs/Web/API/URL) while preserving the original URL.
|
|
|
|
```ts
|
|
import { NextResponse } from 'next/server'
|
|
|
|
// Incoming request: /about, browser shows /about
|
|
// Rewritten request: /proxy, browser shows /about
|
|
return NextResponse.rewrite(new URL('/proxy', request.url))
|
|
```
|
|
|
|
## `next()`
|
|
|
|
The `next()` method is useful for Middleware, as it allows you to return early and continue routing.
|
|
|
|
```ts
|
|
import { NextResponse } from 'next/server'
|
|
|
|
return NextResponse.next()
|
|
```
|
|
|
|
You can also forward `headers` when producing the response:
|
|
|
|
```ts
|
|
import { NextResponse } from 'next/server'
|
|
|
|
// Given an incoming request...
|
|
const newHeaders = new Headers(request.headers)
|
|
// Add a new header
|
|
newHeaders.set('x-version', '123')
|
|
// And produce a response with the new headers
|
|
return NextResponse.next({
|
|
request: {
|
|
// New request headers
|
|
headers: newHeaders,
|
|
},
|
|
})
|
|
```
|