nextjs
config_error
ai_generated
true
Error: Invalid middleware matcher. The pattern 'X' is not a valid regex or path pattern.
ID: nextjs/invalid-middleware-matcher-regex
95%Fix Rate
90%Confidence
1Evidence
2023-09-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
Root Cause
The matcher array in middleware.config contains a malformed regex or path pattern that Next.js cannot parse.
generic中文
middleware.config 中的匹配器数组包含一个格式错误的正则表达式或路径模式,Next.js 无法解析。
Official Documentation
https://nextjs.org/docs/app/building-your-application/routing/middleware#matcherWorkarounds
-
95% success Use the correct glob syntax for path matching. For example, to match all API routes: export const config = { matcher: '/api/:path*', }; For multiple patterns, use an array: export const config = { matcher: ['/api/:path*', '/admin/:path*'], };
Use the correct glob syntax for path matching. For example, to match all API routes: export const config = { matcher: '/api/:path*', }; For multiple patterns, use an array: export const config = { matcher: ['/api/:path*', '/admin/:path*'], }; -
85% success If you need complex matching, use a custom middleware function that checks the request URL manually instead of relying on matcher.
If you need complex matching, use a custom middleware function that checks the request URL manually instead of relying on matcher.
-
90% success Validate your pattern using the Next.js documentation: patterns use a simplified glob where '*' matches any path segment and ':param' captures named parameters.
Validate your pattern using the Next.js documentation: patterns use a simplified glob where '*' matches any path segment and ':param' captures named parameters.
中文步骤
使用正确的 glob 语法进行路径匹配。例如,匹配所有 API 路由: export const config = { matcher: '/api/:path*', }; 对于多个模式,使用数组: export const config = { matcher: ['/api/:path*', '/admin/:path*'], };如果需要复杂匹配,使用自定义中间件函数手动检查请求 URL,而不是依赖匹配器。
使用 Next.js 文档验证你的模式:模式使用简化的 glob,其中 '*' 匹配任何路径段,':param' 捕获命名参数。
Dead Ends
Common approaches that don't work:
-
80% fail
Next.js matcher patterns use a simplified glob syntax, not full regex. Forward slashes are reserved for path segments.
-
70% fail
Characters like '.' or '*' need escaping in regex but Next.js matcher uses glob where '*' is a wildcard, causing ambiguity.
-
50% fail
An empty array matches no routes, effectively disabling the middleware, which may not be the intended behavior.