Skip to content

Commit be9611d

Browse files
authored
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
1 parent 99c6fe2 commit be9611d

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

.github/workflows/publish-production.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
name: Build
2929
env:
3030
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
ENABLE_LAST_MOD_IN_SITEMAP: true
3132
- run: npx wrangler deploy
3233
name: Deploy to Cloudflare Workers
3334
env:

astro.config.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import liveCode from "astro-live-code";
66
import starlightLinksValidator from "starlight-links-validator";
77
import starlightScrollToTop from "starlight-scroll-to-top";
88
import icon from "astro-icon";
9-
import sitemap from "@astrojs/sitemap";
9+
import sitemap, { type SitemapItem } from "@astrojs/sitemap";
1010
import react from "@astrojs/react";
1111

1212
import { readdir } from "fs/promises";
@@ -59,7 +59,10 @@ async function autogenStyles() {
5959
const sidebar = await autogenSections();
6060
const customCss = await autogenStyles();
6161

62-
const runLinkCheck = process.env.RUN_LINK_CHECK || false;
62+
const RUN_LINK_CHECK =
63+
process.env.RUN_LINK_CHECK?.toLowerCase() === "true" || false;
64+
const ENABLE_LAST_MOD_IN_SITEMAP =
65+
process.env.ENABLE_LAST_MOD_IN_SITEMAP?.toLowerCase() === "true";
6366

6467
/**
6568
* Get the last Git modification date for a file
@@ -107,6 +110,22 @@ function urlToFilePath(url: string): string | null {
107110
}
108111
}
109112

113+
function addLastModDate(item: SitemapItem) {
114+
const filePath = urlToFilePath(item.url);
115+
if (filePath) {
116+
const gitDate = getGitLastModified(filePath);
117+
if (gitDate) {
118+
item.lastmod = gitDate;
119+
}
120+
} else {
121+
console.warn(
122+
`[sitemap] Could not find last modified for ${item.url} - setting to now`,
123+
);
124+
item.lastmod = new Date().toISOString();
125+
}
126+
return item;
127+
}
128+
110129
// https://astro.build/config
111130
export default defineConfig({
112131
site: "https://developers.cloudflare.com",
@@ -176,7 +195,7 @@ export default defineConfig({
176195
customCss,
177196
pagination: false,
178197
plugins: [
179-
...(runLinkCheck
198+
...(RUN_LINK_CHECK
180199
? [
181200
starlightLinksValidator({
182201
errorOnInvalidHashes: false,
@@ -242,19 +261,7 @@ export default defineConfig({
242261
return true;
243262
},
244263
serialize(item) {
245-
const filePath = urlToFilePath(item.url);
246-
if (filePath) {
247-
const gitDate = getGitLastModified(filePath);
248-
if (gitDate) {
249-
item.lastmod = gitDate;
250-
}
251-
} else {
252-
console.warn(
253-
`[sitemap] Could not find last modified for ${item.url} - setting to now`,
254-
);
255-
item.lastmod = new Date().toISOString();
256-
}
257-
return item;
264+
return ENABLE_LAST_MOD_IN_SITEMAP ? addLastModDate(item) : item;
258265
},
259266
}),
260267
react(),

0 commit comments

Comments
 (0)