Commit 7391917b9e

7391917b9e21bf297fdafa8455bfc3e9db31e9c9

parent: e9479925a2

Unsigned

Alicia Sykes <alicia@omg.lol> · 2026-08-08 21:45 UTC

Real value for lastmod in sitemap according to changelog

Layout: unified · split

web/astro.config.mjs +10 −1
@@ -5,6 +5,7 @@ import svelte from '@astrojs/svelte';
55import partytown from '@astrojs/partytown';
66import sitemap from '@astrojs/sitemap';
77import { printSummary } from './src/utils/logger.ts';
8import { fetchLastmod } from './src/utils/fetch-lastmod.ts';
89
910// Deploy target (vercel | netlify | cloudflare | node). Default: vercel.
1011const env = { ...import.meta.env, ...process.env };
@@ -41,11 +42,19 @@ const buildLogger = {
4142 },
4243};
4344
45// Get accurate lasmod date for each page from changelog, for sitemap
46const lastmod = await fetchLastmod();
47
48const serialize = (item) => {
49 const changed = lastmod[new URL(item.url).pathname];
50 return changed ? { ...item, lastmod: new Date(changed) } : item;
51};
52
4453export default defineConfig({
4554 output,
4655 site,
4756 adapter: await loadAdapter(),
48 integrations: [svelte(), partytown(), sitemap(), buildLogger],
57 integrations: [svelte(), partytown(), sitemap({ serialize }), buildLogger],
4958 vite: {
5059 css: {
5160 preprocessorOptions: {
web/src/utils/fetch-lastmod.ts added +58
@@ -0,0 +1,58 @@
1import { fetchChangelog } from './fetch-changelog';
2import { slugify } from './fetch-data';
3import type { ChangelogEntry } from './fetch-changelog';
4
5type Crumbs = Array<string | undefined>;
6
7/**
8 * Really over-complicated way to get the last modified date from changelog
9 * for any given page, for the sitemap to be accurate and correct
10 * (even tho no one uses the lastmod field)
11 */
12const listChanged = ({
13 services,
14 sections,
15 categories,
16}: ChangelogEntry['changes']): Crumbs[] => [
17 ...[
18 ...(services?.added || []),
19 ...(services?.removed || []),
20 ...(services?.modified || []),
21 ].map((s) => [s.category, s.section, s.name]),
22 ...[...(services?.moved || []), ...(services?.renamed || [])].flatMap((s) => [
23 [s.from.category, s.from.section, s.name],
24 [s.to.category, s.to.section, s.name],
25 ]),
26 ...[...(sections?.added || []), ...(sections?.removed || [])].map((s) => [
27 s.category,
28 s.name,
29 ]),
30 ...(sections?.moved || []).flatMap((s) => [
31 [s.from.category, s.from.section],
32 [s.to.category, s.to.section],
33 ]),
34 ...(categories?.added || []).map((c) => [c]),
35 ...(categories?.removed || []).map((c) => [c]),
36];
37
38/** Every ancestor page a change touches, e.g. `/essentials/`, `/essentials/browsers/`. */
39const ancestorPaths = (crumbs: Crumbs) => {
40 const parts = crumbs.filter(Boolean).map((part) => slugify(part as string));
41 return parts.map((_, index) => `/${parts.slice(0, index + 1).join('/')}/`);
42};
43
44/** Maps each listing page to the date its content last changed, for sitemap `lastmod`. */
45export const fetchLastmod = async (): Promise<Record<string, string>> => {
46 const { entries } = await fetchChangelog();
47 const lastmod: Record<string, string> = {};
48 entries.forEach((entry) => {
49 listChanged(entry.changes)
50 .flatMap(ancestorPaths)
51 .forEach((path) => {
52 if (!lastmod[path] || entry.date > lastmod[path]) {
53 lastmod[path] = entry.date;
54 }
55 });
56 });
57 return lastmod;
58};