nextjs config_error ai_generated true

错误:无效的中间件匹配器。模式 'X' 不是有效的正则表达式或路径模式。

Error: Invalid middleware matcher. The pattern 'X' is not a valid regex or path pattern.

ID: nextjs/invalid-middleware-matcher-regex

其他格式: JSON · Markdown 中文 · English
95%修复率
90%置信度
1证据数
2023-09-05首次发现

版本兼容性

版本状态引入弃用备注
[email protected] active
[email protected] active
[email protected] active

根因分析

middleware.config 中的匹配器数组包含一个格式错误的正则表达式或路径模式,Next.js 无法解析。

English

The matcher array in middleware.config contains a malformed regex or path pattern that Next.js cannot parse.

generic

官方文档

https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher

解决方案

  1. 使用正确的 glob 语法进行路径匹配。例如,匹配所有 API 路由:
    
    export const config = {
      matcher: '/api/:path*',
    };
    
    对于多个模式,使用数组:
    export const config = {
      matcher: ['/api/:path*', '/admin/:path*'],
    };
  2. 如果需要复杂匹配,使用自定义中间件函数手动检查请求 URL,而不是依赖匹配器。
  3. 使用 Next.js 文档验证你的模式:模式使用简化的 glob,其中 '*' 匹配任何路径段,':param' 捕获命名参数。

无效尝试

常见但无效的做法:

  1. 80% 失败

    Next.js matcher patterns use a simplified glob syntax, not full regex. Forward slashes are reserved for path segments.

  2. 70% 失败

    Characters like '.' or '*' need escaping in regex but Next.js matcher uses glob where '*' is a wildcard, causing ambiguity.

  3. 50% 失败

    An empty array matches no routes, effectively disabling the middleware, which may not be the intended behavior.