From cf3bb583d98aeb4a1cfa75b6ce0c9bfc024c4358 Mon Sep 17 00:00:00 2001 From: Steven Tey Date: Fri, 4 Nov 2022 10:20:53 -0700 Subject: [PATCH] Included negative matcher docs in upgrade guide (#42489) ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) --- errors/middleware-upgrade-guide.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/errors/middleware-upgrade-guide.md b/errors/middleware-upgrade-guide.md index 4bcf66ce0ecd..6d10bf8472ee 100644 --- a/errors/middleware-upgrade-guide.md +++ b/errors/middleware-upgrade-guide.md @@ -67,6 +67,23 @@ export const config = { } ``` +The matcher config also allows full regex so matching like negative lookaheads or character matching is supported. An example of a negative lookahead to match all except specific paths can be seen here: + +```typescript +// /middleware.js +export const config = { + matcher: [ + /* + * Match all request paths except for the ones starting with: + * - api (API routes) + * - _next/static (static files) + * - favicon.ico (favicon file) + */ + '/((?!api|_next/static|favicon.ico).*)', + ], +} +``` + While the config option is preferred since it doesn't get invoked on every request, you can also use conditional statements to only run the Middleware when it matches specific paths. One advantage of using conditionals is defining explicit ordering for when Middleware executes. The following example shows how you can merge two previously nested Middleware: ```typescript