feat(ci): PR 防呆——docs rename/delete 时自动校验 redirect 覆盖#353
Merged
Conversation
PR 中 content/docs/** 文件被 rename/delete 时,CI 自动校验旧 URL 是否已在 next.config.mjs 的 redirects 里有 source 覆盖(精确匹配或 :path* wildcard)。未覆盖则 exit 1 阻断合并,避免搜索引擎收录链接变 404。 - scripts/check-doc-paths.mjs:核心校验脚本(算法:git diff --name-status 提取旧路径 → URL 归一化 → source 覆盖检查 → 报错输出) - .github/workflows/content-check.yml:追加 Check doc path coverage step 豁免机制:在 next.config.mjs 里写 "# no-redirect-needed: <path>" 注释可跳过 对应路径的检查。双语文件(.en.md / .en.mdx)去重到同一 URL 只检查一次。
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a CI guard to prevent accidental SEO-breaking 404s when docs under content/docs/** are renamed or deleted, by requiring that the old URLs are covered by a 301 redirect rule in next.config.mjs.
Changes:
- Add
scripts/check-doc-paths.mjsto detect renamed/deleted doc pages in a PR and validate redirectsourcecoverage (including/:path*prefix rules). - Extend
content-check.ymlto run the new redirect-coverage check after the existing frontmatter validation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| scripts/check-doc-paths.mjs | New script that derives old doc URLs from git diff and verifies they are covered by next.config.mjs redirect sources (with an exemption mechanism). |
| .github/workflows/content-check.yml | Adds a blocking CI step to execute the new doc redirect coverage check on PRs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+19
to
+21
| * 豁免 | ||
| * 如果文件注释里含有 # no-redirect-needed,该路径跳过检查(用于确认不需要兜底的场景)。 | ||
| * 在 next.config.mjs 的 redirects 块里写这个注释即可豁免对应旧路径。 |
Comment on lines
+39
to
+46
| try { | ||
| // --diff-filter=RD:只取 Renamed 和 Deleted | ||
| // --name-status:输出 "R100\told\tnew" 或 "D\tpath",这样 rename 时能拿到旧路径 | ||
| // --name-only 对 rename 只给新路径,无法检查旧 URL,必须用 --name-status | ||
| output = execSync( | ||
| `git diff origin/${BASE_REF}...HEAD --diff-filter=RD --name-status -- 'content/docs/**'`, | ||
| { cwd: ROOT, encoding: "utf-8" }, | ||
| ).trim(); |
Comment on lines
+55
to
+56
| console.log("⚠️ 无法获取 git diff,跳过 doc path 检查"); | ||
| process.exit(0); |
Comment on lines
+78
to
+79
| return oldPaths.filter((f) => f.startsWith("content/docs/")); | ||
| } |
Comment on lines
+89
to
+92
| - name: Check doc path coverage (301 redirect) | ||
| run: node scripts/check-doc-paths.mjs | ||
| env: | ||
| GITHUB_BASE_REF: ${{ github.base_ref }} |
Comment on lines
+147
to
+150
| /** | ||
| * 如果 next.config.mjs 里存在注释 "# no-redirect-needed: <url>"(或包含该 URL 的行), | ||
| * 则该 URL 豁免检查。格式宽松:只要注释行包含 no-redirect-needed 和该 url 片段即算豁免。 | ||
| */ |
Comment on lines
+200
to
+202
| console.error( | ||
| ` 请在 next.config.mjs 加 redirect,或确认此路径无需兜底(加注释 # no-redirect-needed: ${url.replace(/^\//, "")})`, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
scripts/check-doc-paths.mjs:当 PR 中content/docs/**有文件被 rename 或 delete 时,CI 自动检查旧 URL 是否已在next.config.mjs的 redirects 里有 source 覆盖.github/workflows/content-check.yml:在 frontmatter 检查后追加Check doc path coverage (301 redirect)step(拦截性,exit 1 阻断合并)算法
git diff origin/BASE_REF...HEAD --diff-filter=RD --name-status提取所有被 rename/delete 的content/docs/**旧路径content/前缀、去.en.mdx/.mdx后缀、去/index结尾).en.md/.en.mdx)归一化后去重,只检查一次next.config.mjs里是否有精确匹配的 source 或:path*wildcard 前缀覆盖豁免
在
next.config.mjs里写注释# no-redirect-needed: <path>可跳过对应路径(用于确认不需要兜底的场景)。Smoke test 结果
Test plan
pnpm test51 个测试全绿Check doc path coveragestep 出现在 Actions 里🤖 Generated with Claude Code