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/middleware.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,11 @@ Next 12 introduces a new feature called [Middleware](https://nextjs.org/docs/mid
4
4
a request has finished processing. Middleware can be used to modify the request or replace the response. For example, it
5
5
can change headers, rewrite the request path, or return a different response entirely.
6
6
7
-
Next.js Middleware can run either in an edge function or at the origin. On Netlify, middleware runs at the origin as
7
+
Next.js Middleware can run either in an edge function (highly recommended for version 12.2+ as ISR will not work otherwise) or at the origin. On Netlify, middleware runs at the origin as
8
8
part of the normal Next.js server.
9
9
10
+
If you'd like to run Middleware at the edge, set the environment variable `NEXT_USE_NETLIFY_EDGE` to `true`.
11
+
10
12
## How to deploy Next 12 middleware
11
13
12
14
Next 12 Middleware works out of the box with Netlify, and most functions will work unchanged. See
@@ -15,8 +17,7 @@ workarounds that are currently required for some features during the beta period
15
17
16
18
### `geo`
17
19
18
-
When running at the origin, Next.js does not populate the `request.geo` object. Fortunately there is a one line fix to
19
-
get the visitor's country:
20
+
When running at the origin, Next.js does not populate the `request.geo` object as part of the [NextRequest](https://nextjs.org/docs/api-reference/next/server#nextrequest). Fortunately there is a one line fix to get the visitor's country:
20
21
21
22
```typescript
22
23
exportasyncfunction middleware(req:NextRequest) {
@@ -40,9 +41,9 @@ export async function middleware(req: NextRequest) {
40
41
}
41
42
```
42
43
43
-
## Caveats
44
+
## Caveats when middleware is run at the origin
44
45
45
-
Because the middleware runs at the origin, it is run _after_ Netlify rewrites and redirects. If a static file is served
46
+
When the middleware runs at the origin, it is run _after_ Netlify rewrites and redirects. If a static file is served
46
47
by the Netlify CDN then the middleware is never run, as middleware only runs when a page is served by Next.js. This
47
48
means that any pages that match middleware routes are served from the origin rather than the CDN.
0 commit comments