Skip to content

Commit 3b4a8c4

Browse files
feat: add docs on middleware (#795)
* feat: add docs on midldeware * Apply suggestions from code review Co-authored-by: Rachael Stavchansky <rachael@netlify.com> Co-authored-by: Rachael Stavchansky <rachael@netlify.com>
1 parent 1d52bf3 commit 3b4a8c4

File tree

2 files changed

+101
-11
lines changed

2 files changed

+101
-11
lines changed

README.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
# Essential Next.js Build Plugin (beta)
44

5-
6-
:warning: This is the beta version of the Essential Next.js plugin. For the stable version, see [Essential Next.js plugin v3](https://github.com/netlify/netlify-plugin-nextjs/tree/v3#readme) :warning:
5+
:warning: This is the beta version of the Essential Next.js plugin. For the stable version, refer to
6+
[Essential Next.js plugin v3](https://github.com/netlify/netlify-plugin-nextjs/tree/v3#readme) :warning:
77

88
<p align="center">
99
<a aria-label="npm version" href="https://www.npmjs.com/package/@netlify/plugin-nextjs">
@@ -16,16 +16,19 @@
1616

1717
## What's new in this version
1818

19-
Version 4 is a complete rewrite of the Essential Next.js plugin. For full details of everything that's new, see [the v4 release notes](https://github.com/netlify/netlify-plugin-nextjs/blob/main/docs/release-notes/v4.md)
19+
Version 4 is a complete rewrite of the Essential Next.js plugin. For full details of everything that's new, check out
20+
[the v4 release notes](https://github.com/netlify/netlify-plugin-nextjs/blob/main/docs/release-notes/v4.md)
2021

2122
## Installing the beta
2223

23-
2424
- Install the module:
25+
2526
```shell
2627
npm install -D @netlify/plugin-nextjs@beta
2728
```
28-
- Change the `publish` directory to `.next` and add the plugin to `netlify.toml` if not already installed:
29+
30+
- Change the `publish` directory to `.next` and add the plugin to `netlify.toml` if not already installed:
31+
2932
```toml
3033
[build]
3134
publish = ".next"
@@ -34,16 +37,27 @@ publish = ".next"
3437
package = "@netlify/plugin-nextjs"
3538
```
3639

37-
If you previously set a custom `distDir` in your `next.config.js`, or set `node_bundler` or `external_node_modules` in your `netlify.toml` these are no longer needed and can be removed.
40+
If you previously set a custom `distDir` in your `next.config.js`, or set `node_bundler` or `external_node_modules` in
41+
your `netlify.toml` these are no longer needed and can be removed.
42+
43+
The `serverless` and `experimental-serverless-trace` targets are deprecated in Next 12, and all builds with this plugin
44+
will now use the default `server` target. If you previously set the target in your `next.config.js`, you should remove
45+
it.
3846

39-
The `serverless` and `experimental-serverless-trace` targets are deprecated in Next 12, and all builds with this plugin will now use the default `server` target. If you previously set the target in your `next.config.js`, you should remove it.
47+
If you are using a monorepo you will need to change `publish` to point to the full path to the built `.next` directory,
48+
which may be in a subdirectory. If you have changed your `distDir` then it will need to match that.
4049

41-
If you are using a monorepo you will need to change `publish` to point to the full path to the built `.next` directory, which may be in a subdirectory. If you have changed your `distDir` then it will need to match that.
50+
If you are using Nx, then you will need to point `publish` to the folder inside `dist`, e.g. `dist/apps/myapp/.next`.
4251

43-
If you are using Nx, then you will need to point `publish` to the folder inside `dist`, e.g. `dist/apps/myapp/.next`.
52+
If you currently use redirects or rewrites on your site, see
53+
[the Rewrites and Redirects guide](https://github.com/netlify/netlify-plugin-nextjs/blob/main/docs/redirects-rewrites.md)
54+
for information on changes to how they are handled in this version.
4455

45-
If you currently use redirects or rewrites on your site, see [the Rewrites and Redirects guide](https://github.com/netlify/netlify-plugin-nextjs/blob/main/docs/redirects-rewrites.md) for information on changes to how they are handled in this version.
56+
If you want to use Next 12's beta Middleware feature, this will mostly work as expected but please
57+
[read the docs on some caveats and workarounds](https://github.com/netlify/netlify-plugin-nextjs/blob/main/docs/middleware.md)
58+
that are currently needed.
4659

4760
## Beta feedback
4861

49-
Please share any thoughts, feedback or questions about the beta [in our discussion](https://github.com/netlify/netlify-plugin-nextjs/discussions/706).
62+
Please share any thoughts, feedback or questions about the beta
63+
[in our discussion](https://github.com/netlify/netlify-plugin-nextjs/discussions/706).

docs/middleware.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Using Next 12 middleware on Netlify
2+
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+
example, it can change headers, rewrite the request path, or return a different response entirely.
6+
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+
of the normal Next.js server.
9+
10+
## How to deploy Next 12 middleware
11+
12+
Next 12 Middleware works out of the box with Netlify, and most functions will work unchanged. See
13+
[the middleware docs](https://nextjs.org/docs/middleware) for details of how to create them. There are however a few
14+
workarounds that are currently required for some features during the beta period:
15+
16+
### `geo`
17+
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+
the visitor's country:
20+
21+
```typescript
22+
export async function middleware(req: NextRequest) {
23+
// Add this line
24+
req.geo.country = req.headers.get('x-country')
25+
26+
// The rest of your middleware goes here
27+
}
28+
```
29+
30+
### `ip`
31+
32+
Next.js also does not populate the `req.ip` value when running at the origin. There is another one line fix for this:
33+
34+
```typescript
35+
export async function middleware(req: NextRequest) {
36+
// Add this line
37+
req.ip = req.headers.get('x-nf-client-connection-ip')
38+
39+
// The rest of your middleware goes here
40+
}
41+
```
42+
43+
## Caveats
44+
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+
`EXPERIMENTAL_MOVE_STATIC_FILES` option, as this causes statically-generated pages to be served by the Netlify CDN
48+
before any middleware can be run.
49+
50+
There is currently [a bug in Next.js](https://github.com/vercel/next.js/issues/31179) that causes a proxy loop if you
51+
try to rewrite to a URL with a host other than localhost. If you are using a pattern like this:
52+
53+
```typescript
54+
export function middleware(req: NextRequest) {
55+
// Change the `nextUrl` property in some way
56+
req.nextUrl = req.nextUrl.replace('something', 'somethingelse')
57+
// ...then rewrite to the changed URL
58+
return NextResponse.rewrite(req.nextUrl)
59+
}
60+
```
61+
62+
...then you need to set the `nextUrl.host` to `localhost`:
63+
64+
```typescript
65+
export function middleware(req: NextRequest) {
66+
// Change the `nextUrl` property in some way
67+
req.nextUrl = req.nextUrl.replace('something', 'somethingelse')
68+
req.nextUrl.host = 'localhost'
69+
70+
// ...then rewrite to the changed URL
71+
return NextResponse.rewrite(req.nextUrl)
72+
}
73+
```
74+
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

Comments
 (0)