-
Notifications
You must be signed in to change notification settings - Fork 89
feat: add docs on middleware #795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Using Next 12 middleware on Netlify | ||
|
||
Next 12 introduces a new feature called [Middleware](https://nextjs.org/docs/middleware), in which functions | ||
run before a request has finished processing. Middleware can be used to modify the request or replace the response. For | ||
example, it can change headers, rewrite the request path, or return a different response entirely. | ||
|
||
Next.js Middleware can run either in an edge function or at the origin. On Netlify, middleware runs at the origin as part | ||
of the normal Next.js server. | ||
|
||
## How to deploy Next 12 middleware | ||
|
||
Next 12 Middleware works out of the box with Netlify, and most functions will work unchanged. See | ||
[the middleware docs](https://nextjs.org/docs/middleware) for details of how to create them. There are however a few | ||
workarounds that are currently required for some features during the beta period: | ||
|
||
### `geo` | ||
|
||
When running at the origin, Next.js does not populate the `request.geo` object. Fortunately there is a one line fix to get | ||
the visitor's country: | ||
|
||
```typescript | ||
export async function middleware(req: NextRequest) { | ||
// Add this line | ||
req.geo.country = req.headers.get('x-country') | ||
|
||
// The rest of your middleware goes here | ||
} | ||
``` | ||
|
||
### `ip` | ||
|
||
Next.js also does not populate the `req.ip` value when running at the origin. There is another one line fix for this: | ||
|
||
```typescript | ||
export async function middleware(req: NextRequest) { | ||
// Add this line | ||
req.ip = req.headers.get('x-nf-client-connection-ip') | ||
|
||
// The rest of your middleware goes here | ||
} | ||
``` | ||
|
||
## Caveats | ||
|
||
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 | ||
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 | ||
`EXPERIMENTAL_MOVE_STATIC_FILES` option, as this causes statically-generated pages to be served by the Netlify CDN | ||
before any middleware can be run. | ||
|
||
There is currently [a bug in Next.js](https://github.com/vercel/next.js/issues/31179) that causes a proxy loop if you | ||
try to rewrite to a URL with a host other than localhost. If you are using a pattern like this: | ||
|
||
```typescript | ||
export function middleware(req: NextRequest) { | ||
// Change the `nextUrl` property in some way | ||
req.nextUrl = req.nextUrl.replace('something', 'somethingelse') | ||
// ...then rewrite to the changed URL | ||
return NextResponse.rewrite(req.nextUrl) | ||
} | ||
``` | ||
|
||
...then you need to set the `nextUrl.host` to `localhost`: | ||
|
||
```typescript | ||
export function middleware(req: NextRequest) { | ||
// Change the `nextUrl` property in some way | ||
req.nextUrl = req.nextUrl.replace('something', 'somethingelse') | ||
req.nextUrl.host = 'localhost' | ||
|
||
// ...then rewrite to the changed URL | ||
return NextResponse.rewrite(req.nextUrl) | ||
} | ||
``` | ||
|
||
If you have an issue with Next.js middleware on Netlify while it is beta, particularly if the issue cannot be reproduced when | ||
running locally, then please add a comment to [the Next plugin beta discussion](https://ntl.fyi/next-beta-feedback). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor edits (assuming these are Netlify functions in the first line)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, these are middleware functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it may be good to specify that - for extreme clarity 😄