Skip to content

chore(i18n): 防 matcher 漏排 backend rewrite 再翻车#336

Merged
longsizhuo merged 1 commit intomainfrom
chore/i18n-matcher-defense
May 6, 2026
Merged

chore(i18n): 防 matcher 漏排 backend rewrite 再翻车#336
longsizhuo merged 1 commit intomainfrom
chore/i18n-matcher-defense

Conversation

@longsizhuo
Copy link
Copy Markdown
Member

背景

PR #335 修了登录炸的 hotfix —— proxy.ts 的 next-intl matcher 漏排 /oauth /auth /analytics 三条 rewrite-to-backend 路径,导致 /oauth/render/github 被 redirect 到 /en/oauth/... → rewrite 不匹配 → 404 → 登录全员失败。

按 hotfix 的根因分析,这个 bug 模式还会再发生:任何人加新 next.config.mjs rewrite source 不带 /api/ 前缀但忘了改 proxy.ts matcher,登录链 / OAuth / 后端 API 直通的某条路径就会被 i18n middleware 误吃。

防御

1. tests/proxy-matcher.test.ts(新增)

静态扫 next.config.mjs 拿到所有 rewrite source 列表,解析 proxy.ts matcher 的 negative-lookahead 排除组,对每条 source 的第一段 path 断言它在排除组里:

test.each(rewriteSources)(
  'rewrite source "%s" 的第一段必须在 matcher 排除组里',
  (source) => {
    const seg = firstSegment(source);
    if (!seg || seg.startsWith(":")) return;
    expect(exclusions).toContain(seg);
  },
);

跑过的:16 条现有 rewrite + 2 sanity check (exclusionsapi/_next、能扫到 sources),全过。

2. dev_docs/i18n_url_routing.md 新章节「加新 backend rewrite」

直接告诉后来人:

⚠️ 任何不带 /api/ 前缀的 rewrite source,都要同步更新 proxy.ts 的 matcher 排除组

附 PR #335 事故链 + 正确流程示例。

排查结果

next.config.mjs 所有 16 条 rewrite source 跑一遍,确认 #335 hotfix 之后没有遗漏

source 第一段 排除?
/api/auth/callback/github api
/auth/:path* auth
/analytics/:path* analytics
/api/user-center/:path* api
/oauth/:path* oauth
/api/docs/history api
/api/events /api/events/:path* api
/api/admin/{events,users,community} (×6) api
/api/community/links{,/:path*} api

Test plan

  • pnpm test 19 case 全过
  • 故意改坏 matcher(删 oauth)确认 CI fail(可在 review 时验证)
  • 故意加假 rewrite /foobar/:path* 不动 matcher → 验证 CI fail

PR #335 修了登录炸的 hotfix(next-intl middleware matcher 漏排
/oauth /auth /analytics 三条 rewrite-to-backend 路径)。补防御让同
样的 bug 不再发生:

1. tests/proxy-matcher.test.ts
   - 静态扫 next.config.mjs 提取所有 rewrites() 函数体内的 source
   - 解析 proxy.ts matcher 字符串里 negative-lookahead 的排除组
   - 对每个 source 第一段 path,断言它在排除组里
   - 用 test.each 每条 source 一个 case,错误信息直接指引修法
   - 加新 rewrite 不带 /api/ 前缀但忘改 matcher → CI fail

2. dev_docs/i18n_url_routing.md
   - 新章节「加新 backend rewrite」直接告诉以后写代码的人:
     新增 next.config rewrite 必须同步更新 proxy.ts matcher
   - 列了 PR #335 事故 + 正确流程示例

跑了 pnpm test 19 个 case 全过(含 16 条现有 rewrite + 2 sanity check)。
Copilot AI review requested due to automatic review settings May 6, 2026 17:46
@vercel
Copy link
Copy Markdown

vercel Bot commented May 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
involutionhell-github-io Building Building Preview, Comment May 6, 2026 5:46pm
website-preview Building Building Preview, Comment May 6, 2026 5:46pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant