-
Notifications
You must be signed in to change notification settings - Fork 762
Per-collection sitemaps with index and lastmod #431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ascorbic
merged 4 commits into
emdash-cms:main
from
jdevalk:feat/per-collection-sitemaps
Apr 11, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8291764
feat(core): per-collection sitemaps with index and lastmod
jdevalk 0feeebc
style: format
emdashbot[bot] 95b2da2
chore: add changeset for per-collection sitemaps
jdevalk c196bf6
fix: address Copilot review — separate slug/id, interpolate {id}, tig…
jdevalk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "emdash": minor | ||
| --- | ||
|
|
||
| Per-collection sitemaps with sitemap index and lastmod | ||
|
|
||
| `/sitemap.xml` now serves a `<sitemapindex>` with one child sitemap per SEO-enabled collection. Each collection's sitemap is at `/sitemap-{collection}.xml` with `<lastmod>` on both index entries and individual URLs. Uses the collection's `url_pattern` for correct URL building. |
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
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
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
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
104 changes: 104 additions & 0 deletions
104
packages/core/src/astro/routes/sitemap-[collection].xml.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /** | ||
| * Per-collection sitemap endpoint | ||
| * | ||
| * GET /sitemap-{collection}.xml - Sitemap for a single content collection. | ||
| * | ||
| * Uses the collection's url_pattern to build URLs. Falls back to | ||
| * /{collection}/{slug} when no pattern is configured. | ||
| */ | ||
|
|
||
| import type { APIRoute } from "astro"; | ||
|
|
||
| import { handleSitemapData } from "#api/handlers/seo.js"; | ||
| import { getSiteSettingsWithDb } from "#settings/index.js"; | ||
|
|
||
| export const prerender = false; | ||
|
|
||
| const TRAILING_SLASH_RE = /\/$/; | ||
| const AMP_RE = /&/g; | ||
| const LT_RE = /</g; | ||
| const GT_RE = />/g; | ||
| const QUOT_RE = /"/g; | ||
| const APOS_RE = /'/g; | ||
| const SLUG_PLACEHOLDER = "{slug}"; | ||
| const ID_PLACEHOLDER = "{id}"; | ||
|
|
||
| export const GET: APIRoute = async ({ params, locals, url }) => { | ||
| const { emdash } = locals; | ||
| const collectionSlug = params.collection; | ||
|
|
||
| if (!emdash?.db || !collectionSlug) { | ||
| return new Response("<!-- EmDash not configured -->", { | ||
| status: 500, | ||
| headers: { "Content-Type": "application/xml" }, | ||
| }); | ||
| } | ||
|
|
||
| try { | ||
| const settings = await getSiteSettingsWithDb(emdash.db); | ||
| const siteUrl = (settings.url || url.origin).replace(TRAILING_SLASH_RE, ""); | ||
|
|
||
| const result = await handleSitemapData(emdash.db, collectionSlug); | ||
|
|
||
| if (!result.success || !result.data) { | ||
| return new Response("<!-- Failed to generate sitemap -->", { | ||
| status: 500, | ||
| headers: { "Content-Type": "application/xml" }, | ||
| }); | ||
| } | ||
|
|
||
| const col = result.data.collections[0]; | ||
| if (!col) { | ||
| return new Response("<!-- Collection not found or empty -->", { | ||
| status: 404, | ||
| headers: { "Content-Type": "application/xml" }, | ||
| }); | ||
| } | ||
|
|
||
| const lines: string[] = [ | ||
| '<?xml version="1.0" encoding="UTF-8"?>', | ||
| '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">', | ||
| ]; | ||
|
|
||
| for (const entry of col.entries) { | ||
| const slug = entry.slug || entry.id; | ||
| const path = col.urlPattern | ||
| ? col.urlPattern | ||
| .replace(SLUG_PLACEHOLDER, encodeURIComponent(slug)) | ||
| .replace(ID_PLACEHOLDER, encodeURIComponent(entry.id)) | ||
| : `/${encodeURIComponent(col.collection)}/${encodeURIComponent(slug)}`; | ||
|
|
||
| const loc = `${siteUrl}${path}`; | ||
|
jdevalk marked this conversation as resolved.
|
||
|
|
||
| lines.push(" <url>"); | ||
| lines.push(` <loc>${escapeXml(loc)}</loc>`); | ||
| lines.push(` <lastmod>${escapeXml(entry.updatedAt)}</lastmod>`); | ||
| lines.push(" </url>"); | ||
| } | ||
|
|
||
| lines.push("</urlset>"); | ||
|
|
||
| return new Response(lines.join("\n"), { | ||
| status: 200, | ||
| headers: { | ||
| "Content-Type": "application/xml; charset=utf-8", | ||
| "Cache-Control": "public, max-age=3600", | ||
| }, | ||
| }); | ||
| } catch { | ||
| return new Response("<!-- Internal error generating sitemap -->", { | ||
| status: 500, | ||
| headers: { "Content-Type": "application/xml" }, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| /** Escape special XML characters in a string */ | ||
| function escapeXml(str: string): string { | ||
| return str | ||
| .replace(AMP_RE, "&") | ||
| .replace(LT_RE, "<") | ||
| .replace(GT_RE, ">") | ||
| .replace(QUOT_RE, """) | ||
| .replace(APOS_RE, "'"); | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.