Skip to content

fix(seo): 统一全站多语言路由、canonical、sitemap 与 llms.txt 为带尾斜杠零跳转 URL - #30

Merged
WangEn merged 1 commit into
mainfrom
fix/trailing-slash-urls
Nov 24, 2026
Merged

WangEn merged 1 commit into
mainfrom
fix/trailing-slash-urls

Conversation

@WangEn

@WangEn WangEn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

背景与问题

线上 Coolify / Nixpacks 静态服务器通过目录索引(dist/<route>/index.html)托管 92 条多语言 SSG 路由:

  • 当爬虫或用户访问无尾斜杠地址(如 https://moklu.app/zh/merge-images)时,源站返回 301 -> http://moklu.app/zh/merge-images/,随后 Cloudflare 再 301 -> https://moklu.app/zh/merge-images/(共 2 次重定向);
  • 而页面 <link rel="canonical">、hreflang、JSON-LD、sitemap.xml 与 llms.txt 此前输出无尾斜杠地址,造成规范网址与最终落地 URL 互指冲突,易在 Google Search Console 触发「网页会自动重定向 / 规范网址冲突」并损耗抓取配额。

改动摘要

  1. 统一尾斜杠规范 URL(零跳转直达 HTTP 200):
    • src/seo/routes.ts:buildLocalizedPath 与 buildCanonicalUrl 将所有非根子路由统一规范为带尾斜杠格式(/、/zh/、/zh/merge-images/、/studio/ 等),同时 resolvePathname 继续兼容有/无尾斜杠入站路径;
    • src/seo/ssg.ts:getRouteRelativeHtmlPath 使用 split('/').filter(Boolean) 稳健解析目录段,保持 dist/<route>/index.html 输出结构不变;
    • 自动驱动 <link rel="canonical">、og:url、5 条 hreflang、JSON-LD(BreadcrumbList / WebPage / WebApplication)、sitemap.xml(全部 92 条 <loc>)、llms.txt 及站内所有 SPA 导航链接同步切换为尾斜杠地址。
  2. 测试、文档与守护断言更新:
    • 更新 src/seo/seo.test.ts(新增全量 92 条路由与 sitemap.xml <loc> 尾斜杠不变量断言)、src/seo/geo.test.ts、src/components/common/ChunkErrorBoundary.test.tsx、tests/e2e/seo-and-tools.spec.mjs(含无斜杠入站路径归一化到带斜杠 canonical 的回归断言)及 tests/live/*.spec.mjs;
    • 同步更新 src/seo/GEMINI.md、README.md、docs/analytics.md 与 CHANGELOG.md。

验证结果

  • npm run typecheck:0 error
  • npm run lint:0 error / 0 warning
  • npm test:81 文件 / 958 用例全部通过
  • npm run build:92 路由 SSG + sitemap.xml + robots.txt + llms.txt 构建通过
  • node scripts/check-headers.mjs:0 failures

@WangEn
WangEn merged commit 2c5680d into main Nov 24, 2026
3 checks passed
@WangEn
WangEn deleted the fix/trailing-slash-urls branch November 24, 2026 05:04
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