1
1
# Using Next 12 middleware on Netlify
2
2
3
- Next 12 introduces a new feature called [ middleware ] ( https://nextjs.org/docs/middleware ) , which are functions that are
4
- run before a request has finished processing, and can be used to modify the request or replace the response. For
3
+ Next 12 introduces a new feature called [ Middleware ] ( https://nextjs.org/docs/middleware ) , in which functions
4
+ run before a request has finished processing. Middleware can be used to modify the request or replace the response. For
5
5
example, it can change headers, rewrite the request path, or return a different response entirely.
6
6
7
- Next Middleware can run either in an edge function or at the origin. On Netlify, middleware runs at the origin as part
7
+ Next.js Middleware can run either in an edge function or at the origin. On Netlify, middleware runs at the origin as part
8
8
of the normal Next.js server.
9
9
10
10
## How to deploy Next 12 middleware
@@ -15,7 +15,7 @@ workarounds that are currently required for some features during the beta period
15
15
16
16
### ` geo `
17
17
18
- When running at the origin, Next does not populate the ` request.geo ` object. Fortunately there is a one line fix to get
18
+ When running at the origin, Next.js does not populate the ` request.geo ` object. Fortunately there is a one line fix to get
19
19
the visitor's country:
20
20
21
21
``` typescript
@@ -42,8 +42,8 @@ export async function middleware(req: NextRequest) {
42
42
43
43
## Caveats
44
44
45
- Because the middleware runs at the origin, it is run _ after_ Netlify rewrites and redirects. If a static file is served
46
- then it is never run, as it is only run when a page is served by Next.js. This means that it should not be used with the
45
+ Because the middleware runs at the origin, it is run _ after_ Netlify rewrites and redirects. If a static file is served by the Netlify CDN
46
+ then the middleware is never run, as middleware only runs when a page is served by Next.js. This means that middleware should not be used with the
47
47
` EXPERIMENTAL_MOVE_STATIC_FILES ` option, as this causes statically-generated pages to be served by the Netlify CDN
48
48
before any middleware can be run.
49
49
@@ -72,5 +72,5 @@ export function middleware(req: NextRequest) {
72
72
}
73
73
```
74
74
75
- If you have any issues with middleware on Netlify while it is beta, particularly if they cannot be reproduced when
76
- running locally, then please add a comment to [ the Next plugin beta discussion] ( https://ntl.fyi/next-beta-feedback )
75
+ If you have an issue with Next.js middleware on Netlify while it is beta, particularly if the issue cannot be reproduced when
76
+ running locally, then please add a comment to [ the Next plugin beta discussion] ( https://ntl.fyi/next-beta-feedback ) .
0 commit comments