From 710da4360167252d19e732ebd7db150aef95e94f Mon Sep 17 00:00:00 2001 From: Steven Tey Date: Tue, 6 Sep 2022 12:40:19 -0500 Subject: [PATCH 1/3] Added "negative matcher" documentation --- docs/advanced-features/middleware.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/advanced-features/middleware.md b/docs/advanced-features/middleware.md index 3a53fa223235..b12e4d7df81a 100644 --- a/docs/advanced-features/middleware.md +++ b/docs/advanced-features/middleware.md @@ -88,6 +88,22 @@ export const config = { } ``` +You can also create a "negative matcher" that matches all paths except for certain paths: + +```js +export const config = { + matcher: [ + /* + * Match all request paths except for the ones starting with: + * - api (API routes) + * - static (static files) + * - favicon.ico (favicon file) + */ + "/((?!api|static|favicon.ico).*)", + ], +} +``` + > **Note:** The `matcher` values need to be constants so they can be statically analyzed at build-time. Dynamic values such as variables will be ignored. Configured matchers: From ec3ccee506814fead04ac71a6b66d7b898526051 Mon Sep 17 00:00:00 2001 From: Steven Tey Date: Tue, 6 Sep 2022 14:39:13 -0500 Subject: [PATCH 2/3] Update middleware.md --- docs/advanced-features/middleware.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/advanced-features/middleware.md b/docs/advanced-features/middleware.md index b12e4d7df81a..68bd102bbe29 100644 --- a/docs/advanced-features/middleware.md +++ b/docs/advanced-features/middleware.md @@ -117,8 +117,6 @@ Read more details on [path-to-regexp](https://github.com/pillarjs/path-to-regexp > **Note:** For backward compatibility, Next.js always considers `/public` as `/public/index`. Therefore, a matcher of `/public/:path` will match. -> **Note:** It is not possible to exclude middleware from matching static path starting with `_next/`. This allow enforcing security with middleware. - ### Conditional Statements ```typescript From 15613dce900a5a160d9339512690b069fcb4e88c Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Mon, 12 Sep 2022 18:19:55 -0700 Subject: [PATCH 3/3] mention other regex matching as well --- docs/advanced-features/middleware.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/advanced-features/middleware.md b/docs/advanced-features/middleware.md index 68bd102bbe29..f64cb0ff5f4b 100644 --- a/docs/advanced-features/middleware.md +++ b/docs/advanced-features/middleware.md @@ -88,7 +88,7 @@ export const config = { } ``` -You can also create a "negative matcher" that matches all paths except for certain paths: +The `matcher` config 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: ```js export const config = { @@ -99,7 +99,7 @@ export const config = { * - static (static files) * - favicon.ico (favicon file) */ - "/((?!api|static|favicon.ico).*)", + '/((?!api|static|favicon.ico).*)', ], } ```