You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-reference/next/server.md
+74-2Lines changed: 74 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,13 +70,68 @@ import type { NextFetchEvent } from 'next/server'
70
70
71
71
## NextResponse
72
72
73
-
The `NextResponse` object is an extension of the native [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) interface, with the following added methods and properties:
73
+
The `NextResponse` class extends the native [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) interface, with the following:
74
+
75
+
### Public methods
76
+
77
+
Public methods are available on an instance of the `NextResponse` class. Depending on your use case, you can create an instance and assign to a variable, then access the following public methods:
74
78
75
79
-`cookies` - An object with the cookies in the `Response`
76
-
-`cookie` - Set a cookie in the `Response`
80
+
-`cookie()` - Set a cookie in the `Response`
81
+
-`clearCookie()` - Accepts a `cookie` and clears it
82
+
83
+
```ts
84
+
import { NextResponse } from'next/server'
85
+
importtype { NextRequest } from'next/server'
86
+
87
+
exportfunction middleware(request:NextRequest) {
88
+
// create an instance of the class to access the public methods. This uses `next()`,
89
+
// you could use `redirect()` or `rewrite()` as well
90
+
let response =NextResponse.next()
91
+
// get the cookies from the request
92
+
let cookieFromRequest =request.cookies['my-cookie']
All methods above return a `NextResponse` object that only takes effect if it's returned in the middleware function.
82
137
@@ -86,6 +141,23 @@ All methods above return a `NextResponse` object that only takes effect if it's
86
141
import { NextResponse } from'next/server'
87
142
```
88
143
144
+
### Setting the cookie before a redirect
145
+
146
+
In order to set the `cookie`_before_ a redirect, you can create an instance of `NextResponse`, then access the `cookie` method on the instance, before returning the response.
147
+
148
+
Note that there is a [Chrome bug](https://bugs.chromium.org/p/chromium/issues/detail?id=696204) which means the entire redirect chain **must** be from the same origin, if they are from different origins, then the `cookie` might be missing until a refresh.
149
+
150
+
```ts
151
+
import { NextResponse } from'next/server'
152
+
importtype { NextRequest } from'next/server'
153
+
154
+
exportfunction middleware(req:NextRequest) {
155
+
const res =NextResponse.redirect('/') // creates an actual instance
156
+
res.cookie('hello', 'world') // can be called on an instance
157
+
returnres
158
+
}
159
+
```
160
+
89
161
### Why does redirect use 307 and 308?
90
162
91
163
When using `redirect()` you may notice that the status codes used are `307` for a temporary redirect, and `308` for a permanent redirect. While traditionally a `302` was used for a temporary redirect, and a `301` for a permanent redirect, many browsers changed the request method of the redirect, from a `POST` to `GET` request when using a `302`, regardless of the origins request method.
0 commit comments