Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import react from "@astrojs/react";

import { readdir } from "fs/promises";
import { fileURLToPath } from "url";
import { execSync } from "child_process";
import { existsSync } from "fs";

import remarkValidateImages from "./src/plugins/remark/validate-images";

Expand Down Expand Up @@ -59,6 +61,52 @@ const customCss = await autogenStyles();

const runLinkCheck = process.env.RUN_LINK_CHECK || false;

/**
* Get the last Git modification date for a file
* @param filePath - Absolute path to the file
* @returns ISO date string or null if not available
*/
function getGitLastModified(filePath: string): string | null {
try {
const result = execSync(`git log -1 --format=%cI -- "${filePath}"`, {
encoding: "utf-8",
}).trim();
return result || null;
} catch (_error) {
return null;
}
}

/**
* Convert a sitemap URL to the corresponding source file path
* @param url - The full URL from the sitemap
* @returns Absolute file path or null if not found
*/
function urlToFilePath(url: string): string | null {
try {
const urlObj = new URL(url);
const pathname = urlObj.pathname.replace(/\/$/, ""); // Remove trailing slash

// Try different file extensions and paths
const possiblePaths = [
`./src/content/docs${pathname}.md`,
`./src/content/docs${pathname}.mdx`,
`./src/content/docs${pathname}/index.md`,
`./src/content/docs${pathname}/index.mdx`,
];

for (const path of possiblePaths) {
if (existsSync(path)) {
return path;
}
}

return null;
} catch (_error) {
return null;
}
}

// https://astro.build/config
export default defineConfig({
site: "https://developers.cloudflare.com",
Expand Down Expand Up @@ -194,7 +242,18 @@ export default defineConfig({
return true;
},
serialize(item) {
item.lastmod = new Date().toISOString();
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;
},
}),
Expand Down
Loading