A custom sitemap endpoint instead of the Astro integration
My site has a 52-line sitemap endpoint instead of Astro’s sitemap integration,
because the integration lists what the build produced, not what I want indexed.
Run against this site on 2026-11-24, its default output had 84 URLs: one draft,
two pages whose canonical is elsewhere, and no lastmod at all. The endpoint
lists 81, each with a date.
What the integration lists by default
I made the decision on 2026-07-27, during the rebuild, and did not measure it.
The note says the integration “derives entries from built pages”. Today I
measured it. In a temporary worktree I added @astrojs/sitemap 3.7.6 with no
options, added one probe draft, and built the site on Astro 7.3.5.
The build wrote 85 HTML pages. This is what each sitemap did with them:
| Measure | Integration, default | My endpoint |
|---|---|---|
| URLs listed | 84 | 81 |
| Draft pages | 1 | 0 |
| Pages with a canonical on another site | 2 | 0 |
URLs with lastmod |
0 | 81 |
The integration left out one page, the 404. It listed all the others, because a page that exists is its only input.
That input is wrong for this site in two ways. A draft here renders at its URL
with noindex, so that I can preview it on the live site. The page exists, so
the integration lists it. A sitemap that lists a noindex page gives a search
engine two opposite instructions. Two of my posts were first published on
another site, and their canonical URL points there. A sitemap is a list of
canonical URLs, so those two do not belong in mine.
Where lastmod comes from
Each post on this site has a revision log in its frontmatter. The endpoint
reads the collection, not the build output, so it can use that log. A post’s
lastmod is the newest of its publish date, its updated date and its
revision dates.
The 81 entries carry 36 distinct dates. Of the 46 posts in the sitemap, 10 have
a lastmod later than their publish date. The largest difference is 84 days.
Without the revision log, those 10 posts would show a date that is wrong, or no
date.
The date has a second consumer. My IndexNow script reads dist/sitemap.xml and
submits only the URLs whose lastmod is in the last 7 days. With no lastmod,
it has nothing to select on.
What the serialize hook can and cannot do
My July note also said the integration “cannot set per-URL lastmod”. That is
too strong, and I am correcting it here. The integration has a serialize
hook. It runs for each URL, it can change the entry, and it can return
undefined to drop the entry.
I wrote one. It took 18 lines in astro.config.mjs. It dropped the draft and
the two cross-posted pages, and it set a lastmod on the posts. The result:
81 URLs, the same 81 as my endpoint. The 46 post dates matched the endpoint,
46 of 46.
The other 35 URLs had no lastmod: 5 fixed pages, the tag index and 29 tag
pages. A tag page’s date is the newest change among its posts. To give it one,
the hook needs the tag index too.
That is the real cost. The config file cannot import the content collection,
because astro:content resolves only inside the build. So my 18 lines read
the .mdx files from disk and parsed the frontmatter with a regular
expression. That is a second parser for data the site already validates with a
schema. The draft rule, the canonical rule and the date rule would each exist
in two places. I wrote last week about
one source of truth for a business rule; this
is the same problem at a smaller size.
The endpoint calls the same getPublishedPosts() function that the writing
page, the feed and llms.txt call. A draft cannot be in the sitemap for the
same reason it cannot be in the feed.
Who reads the file
The sitemap is the second most requested file on the site among the crawlers
I log. From 2026-08-10 to 2026-10-28, my log has 3,175 requests that passed IP
verification. /robots.txt had 1,164 of them. /sitemap.xml had 960, which
is 30%: 784 from ClaudeBot, 97 from Bingbot, 77 from GPTBot and 2 from other
agents. The home page was third, with 190. Googlebot is not in that log.
So the file is read five times as often as the most-read page. An error in it is repeated to every crawler that asks, about 12 times each day.
When the integration is the correct choice
The integration is correct when every built page is a page you want indexed and you have no better date than the build date. That is true for many sites. It also splits the file at 45,000 URLs, which my endpoint does not do and does not need to do at 81.
Mine is the opposite case: a small site where the content collection knows
things about a page that the build output does not. A build check now holds
the rules in place. check-content fails if a draft slug appears in the
sitemap, the feed or llms.txt, and fails if the sitemap lists a page whose
canonical is on another site.
Revisions
- Created from the 2026-07-27 decision entries, the git history of the sitemap endpoint, a build of the site with @astrojs/sitemap 3.7.6 in a temporary worktree, and the verified crawler log from 2026-08-10 to 2026-10-28, all read that day.
- Published in the open Nov 23 slot, one day late; auto-drafted by the publishing run from a spare topic, at the owner's request for a post that day.