Skip to content

fix(sitemap): skip external slugs and guard invalid dates#3806

Merged
tangly1024 merged 2 commits intotangly1024:mainfrom
qianzhu18:contrib/fix-sitemap-external-slug
Apr 30, 2026
Merged

fix(sitemap): skip external slugs and guard invalid dates#3806
tangly1024 merged 2 commits intotangly1024:mainfrom
qianzhu18:contrib/fix-sitemap-external-slug

Conversation

@qianzhu18
Copy link
Copy Markdown
Contributor

@qianzhu18 qianzhu18 commented Feb 21, 2026

Summary

Fixes sitemap URL generation when menu/page slugs are external links, and guards invalid publishDay values to avoid invalid date output.

Closes #3668

Problem

  • External links in slug were appended to site domain, producing invalid URLs like:
    • https://example.com/https://external.com/page
  • Invalid dates could leak Invalid Date behavior into sitemap generation paths.

Root Cause

  • Sitemap builders treated all slugs as relative paths.
  • Date formatting called toISOString() without invalid-date fallback.

Changes

  • Added lib/sitemap-utils.js for shared sitemap normalization:
    • base URL normalization
    • locale normalization
    • URL building with external-host filtering
    • safe date formatting fallback
  • Updated pages/sitemap.xml.js to use normalized URL/date helpers.
  • Updated lib/sitemap.xml.js with the same guards.
  • Added tests:
    • __tests__/lib/sitemap-utils.test.js
    • __tests__/lib/sitemap.xml.test.js

Verification

  • npm test -- __tests__/lib/sitemap-utils.test.js __tests__/lib/sitemap.xml.test.js
  • npm run lint -- --file pages/sitemap.xml.js --file lib/sitemap.xml.js --file lib/sitemap-utils.js --file __tests__/lib/sitemap-utils.test.js --file __tests__/lib/sitemap.xml.test.js

Risk & Compatibility

  • Low risk: changes are scoped to sitemap generation only.
  • Behavior change: external absolute URLs on different host are excluded from sitemap (expected for site-local sitemap entries).

@vercel
Copy link
Copy Markdown

vercel Bot commented Feb 21, 2026

@qianzhu18 is attempting to deploy a commit to the tangly1024's projects Team on Vercel.

A member of the Team first needs to authorize it.

@qianzhu18
Copy link
Copy Markdown
Contributor Author

Heads-up: Vercel checks are failing with "Authorization required to deploy" from my fork environment. I verified this change locally with targeted lint/tests listed in the PR description.

@tangly1024 tangly1024 merged commit d3e02fb into tangly1024:main Apr 30, 2026
3 of 6 checks passed
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.

菜单中指向一个外部链接,生成的sitemap 的URL 被重复拼接导致出现双域名

2 participants