diff --git a/.github/workflows/publish-production.yml b/.github/workflows/publish-production.yml index 63137a4ed5bbe9..a6c76ea5170284 100644 --- a/.github/workflows/publish-production.yml +++ b/.github/workflows/publish-production.yml @@ -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: diff --git a/astro.config.ts b/astro.config.ts index 04b2c976d03e94..4079c9911d5a53 100644 --- a/astro.config.ts +++ b/astro.config.ts @@ -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(),