From cb743f53e7f720d54e9dce944a3ad41617a79f83 Mon Sep 17 00:00:00 2001 From: Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> Date: Wed, 7 Oct 2026 06:39:31 +0900 Subject: astro.config.mjs, package.json, scripts/indexnow.mjs: IndexNowとサイトマップlastmodを追加 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IndexNowのキーファイルと送信スクリプト、npm run indexnow、各ページの最終更新日をlastmodに反映。 --- astro.config.mjs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'astro.config.mjs') diff --git a/astro.config.mjs b/astro.config.mjs index 46174d3..f6dd237 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,3 +1,4 @@ +import { execSync } from "node:child_process"; import { existsSync } from "node:fs"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; @@ -45,6 +46,42 @@ function publicDirIndexPlugin() { }; } +// ページの最終更新日を git の履歴から引く(サイトマップの lastmod 用)。 +function buildGitDateMap() { + const map = new Map(); + try { + const out = execSync("git log --no-merges --pretty=format:__C__%cI --name-only", { + encoding: "utf-8", + maxBuffer: 256 * 1024 * 1024, + }); + let date = ""; + for (const raw of out.split("\n")) { + const line = raw.trim(); + if (line.startsWith("__C__")) { + date = line.slice(5); + } else if (line && date && !map.has(line)) { + map.set(line, date); + } + } + } catch { + // git が使えない環境では lastmod を付けない。 + } + return map; +} +const gitDates = buildGitDateMap(); + +function lastmodFor(url) { + const path = url.replace(/^https?:\/\/[^/]+/, "").replace(/^\/+|\/+$/g, ""); + const bases = path ? [path, `${path}/index`] : ["index"]; + for (const base of bases) { + for (const ext of [".mdx", ".md"]) { + const date = gitDates.get(`src/content/docs/${base}${ext}`); + if (date) return new Date(date); + } + } + return undefined; +} + export default defineConfig({ site: "https://yasutakeyohei.com", base: "/", @@ -61,7 +98,13 @@ export default defineConfig({ plugins: [publicDirIndexPlugin()], }, integrations: [ - sitemap(), + sitemap({ + serialize(item) { + const lastmod = lastmodFor(item.url); + if (lastmod) item.lastmod = lastmod; + return item; + }, + }), updatesIntegration(), ogImagePlugin(), starlight({ -- cgit v1.3.1