Skip to content

Commit add4896

Browse files
committed
feat: only add lastmod during prod build
1 parent 6096032 commit add4896

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ jobs:
6666
env:
6767
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6868
RUN_LINK_CHECK: true
69+
ENABLE_LAST_MOD_IN_SITEMAP: true
6970

7071
- run: npm run check:worker
7172

astro.config.ts

Lines changed: 22 additions & 15 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 runLinkCheck =
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",
@@ -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)