feat: only add lastmod during prod build (#26361)

* feat: only add lastmod during prod build

* refactor: use casing for constant

* fix: enable on prod builds, not pr-to-prod builds
This commit is contained in:
Colby M. White 2025-11-07 13:07:26 -06:00 committed by GitHub
parent 99c6fe285d
commit be9611d332
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 16 deletions

View file

@ -28,6 +28,7 @@ jobs:
name: Build
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ENABLE_LAST_MOD_IN_SITEMAP: true
- run: npx wrangler deploy
name: Deploy to Cloudflare Workers
env:

View file

@ -6,7 +6,7 @@ import liveCode from "astro-live-code";
import starlightLinksValidator from "starlight-links-validator";
import starlightScrollToTop from "starlight-scroll-to-top";
import icon from "astro-icon";
import sitemap from "@astrojs/sitemap";
import sitemap, { type SitemapItem } from "@astrojs/sitemap";
import react from "@astrojs/react";
import { readdir } from "fs/promises";
@ -59,7 +59,10 @@ async function autogenStyles() {
const sidebar = await autogenSections();
const customCss = await autogenStyles();
const runLinkCheck = process.env.RUN_LINK_CHECK || false;
const RUN_LINK_CHECK =
process.env.RUN_LINK_CHECK?.toLowerCase() === "true" || false;
const ENABLE_LAST_MOD_IN_SITEMAP =
process.env.ENABLE_LAST_MOD_IN_SITEMAP?.toLowerCase() === "true";
/**
* Get the last Git modification date for a file
@ -107,6 +110,22 @@ function urlToFilePath(url: string): string | null {
}
}
function addLastModDate(item: SitemapItem) {
const filePath = urlToFilePath(item.url);
if (filePath) {
const gitDate = getGitLastModified(filePath);
if (gitDate) {
item.lastmod = gitDate;
}
} else {
console.warn(
`[sitemap] Could not find last modified for ${item.url} - setting to now`,
);
item.lastmod = new Date().toISOString();
}
return item;
}
// https://astro.build/config
export default defineConfig({
site: "https://developers.cloudflare.com",
@ -176,7 +195,7 @@ export default defineConfig({
customCss,
pagination: false,
plugins: [
...(runLinkCheck
...(RUN_LINK_CHECK
? [
starlightLinksValidator({
errorOnInvalidHashes: false,
@ -242,19 +261,7 @@ export default defineConfig({
return true;
},
serialize(item) {
const filePath = urlToFilePath(item.url);
if (filePath) {
const gitDate = getGitLastModified(filePath);
if (gitDate) {
item.lastmod = gitDate;
}
} else {
console.warn(
`[sitemap] Could not find last modified for ${item.url} - setting to now`,
);
item.lastmod = new Date().toISOString();
}
return item;
return ENABLE_LAST_MOD_IN_SITEMAP ? addLastModDate(item) : item;
},
}),
react(),