Skip to content

Commit 5484da0

Browse files
docs: update notes about running Middleware (#1478)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 4c2aaa0 commit 5484da0

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

docs/middleware.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ Next 12 introduces a new feature called [Middleware](https://nextjs.org/docs/mid
44
a request has finished processing. Middleware can be used to modify the request or replace the response. For example, it
55
can change headers, rewrite the request path, or return a different response entirely.
66

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
88
part of the normal Next.js server.
99

10+
If you'd like to run Middleware at the edge, set the environment variable `NEXT_USE_NETLIFY_EDGE` to `true`.
11+
1012
## How to deploy Next 12 middleware
1113

1214
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
1517

1618
### `geo`
1719

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:
2021

2122
```typescript
2223
export async function middleware(req: NextRequest) {
@@ -40,9 +41,9 @@ export async function middleware(req: NextRequest) {
4041
}
4142
```
4243

43-
## Caveats
44+
## Caveats when middleware is run at the origin
4445

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
4647
by the Netlify CDN then the middleware is never run, as middleware only runs when a page is served by Next.js. This
4748
means that any pages that match middleware routes are served from the origin rather than the CDN.
4849

0 commit comments

Comments
 (0)