Skip to content

feat(docs): not-found 自动解析历史路径,resolver 命中即 301 单跳#354

Merged
longsizhuo merged 1 commit into
mainfrom
feat/docs-not-found-resolver
May 24, 2026
Merged

feat(docs): not-found 自动解析历史路径,resolver 命中即 301 单跳#354
longsizhuo merged 1 commit into
mainfrom
feat/docs-not-found-resolver

Conversation

@longsizhuo
Copy link
Copy Markdown
Member

Summary

  • 新建 app/[locale]/docs/not-found.tsx(segment-level not-found,只对 /[locale]/docs/** 生效):命中旧 URL 时向 /api/docs/resolve 查询 doc_paths 历史表,拿到 301+Location 就 router.replace 到新 URL;端点 404 或超时则显示标准 404 UI
  • next.config.mjs 追加 /api/docs/resolve → backend rewrite

行为

用户访问旧路径(如 /zh/docs/community/dev-tips/git101)
  ↓ Next.js 找不到对应 SSG 页面,触发 segment not-found
  ↓ DocsNotFound 组件挂载,fetch /api/docs/resolve
  ↓ 端点返回 301 + Location: /docs/learn/cs/dev-tips/git101
  ↓ router.replace("/zh/docs/learn/cs/dev-tips/git101")
  → 最终 200,单跳

防重定向环(三层)

机制
前端层 location === strippedPath 时不跳,直接显示 404
端点层 canonical 来自 path_current(实际存在的文件路径),正常不再触发 not-found
超时层 500ms AbortController,端点异常直接降级 404,不白屏等待

验收

依赖 Block 2 端点/api/docs/resolve)merge 到生产后,手动验证以下场景:

  • 旧路径(doc_paths 表有记录)→ 短暂 skeleton → 301 跳到新路径 → 最终 200
  • 真不存在路径 → resolver 404 → 显示 404 UI,无跳转环
  • 正常存在的 docs 页 → 直接渲染,not-found.tsx 不介入
  • resolver 超时(DevTools throttle)→ 500ms 后显示 404 UI

Test plan

  • pnpm build 通过,TypeScript clean,52 vitest 全绿
  • 路由表无回归(/[locale]/docs/[...slug] 仍 SSG,正常 docs 页不受影响)
  • /api/docs/resolve rewrite 本地验证通过(curl 3010 返回 301/404)
  • 正常 docs 页的加载路径完全不经过 not-found.tsx

🤖 Generated with Claude Code

- 新建 app/[locale]/docs/not-found.tsx(segment-level not-found):
  查询 /api/docs/resolve,命中 doc_paths 历史表就 router.replace 到
  新路径,端点返回 404 或 500ms 超时则显示标准 404 UI
- next.config.mjs 追加 /api/docs/resolve → backend rewrite

防重定向环三层:
  前端层:location === strippedPath 时不跳(canonical == current)
  端点层:canonical 来自 path_current(当前文件必然存在)
  超时层:500ms AbortController,端点异常直接降级 404

`router.replace` 而非 `push`,后退不回到 not-found 页。
Copilot AI review requested due to automatic review settings May 24, 2026 17:58
@vercel
Copy link
Copy Markdown

vercel Bot commented May 24, 2026

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

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@longsizhuo longsizhuo merged commit df23329 into main May 24, 2026
7 of 9 checks passed
@longsizhuo longsizhuo deleted the feat/docs-not-found-resolver branch May 24, 2026 18:12
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.

2 participants