-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastro.config.mjs
More file actions
94 lines (85 loc) · 2.87 KB
/
Copy pathastro.config.mjs
File metadata and controls
94 lines (85 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// @ts-check
import { execFileSync } from "node:child_process";
import { existsSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { defineConfig } from "astro/config";
import tailwindcss from "@tailwindcss/vite";
import sitemap from "@astrojs/sitemap";
const REPO_ROOT = fileURLToPath(new URL(".", import.meta.url));
/** URL pathname → source file used for git mtime lookup. */
function pageFileForUrl(pathname) {
if (pathname === "/") return "src/pages/index.astro";
const slug = pathname.replace(/^\/+|\/+$/g, "");
const flat = `src/pages/${slug}.astro`;
if (existsSync(`${REPO_ROOT}${flat}`)) return flat;
return `src/pages/${slug}/index.astro`;
}
const mtimeCache = new Map();
/** Most recent commit time for `file`, or null if unavailable. */
function gitMtime(file) {
if (mtimeCache.has(file)) return mtimeCache.get(file);
const abs = `${REPO_ROOT}${file}`;
if (!existsSync(abs)) {
mtimeCache.set(file, null);
return null;
}
try {
const out = execFileSync(
"git",
["log", "-1", "--format=%cI", "--", file],
{ cwd: REPO_ROOT, encoding: "utf-8" },
).trim();
if (!out) console.warn(`[sitemap] no git history for ${file} — lastmod omitted`);
const value = out ? new Date(out) : null;
mtimeCache.set(file, value);
return value;
} catch (err) {
console.warn(`[sitemap] git lookup failed for ${file} — lastmod omitted (${err?.message ?? err})`);
mtimeCache.set(file, null);
return null;
}
}
/**
* Latest commit across the shared UI (layout, components, styles, data)
* that shapes every page. A page's true lastmod is the newer of its own
* file and this — otherwise a full redesign done in components would leave
* the sitemap claiming pages are unchanged.
*/
const SHARED_PATHS = ["src/components", "src/layouts", "src/styles", "src/data"];
let sharedMtimeCache;
function sharedMtime() {
if (sharedMtimeCache !== undefined) return sharedMtimeCache;
try {
const out = execFileSync(
"git",
["log", "-1", "--format=%cI", "--", ...SHARED_PATHS],
{ cwd: REPO_ROOT, encoding: "utf-8" },
).trim();
sharedMtimeCache = out ? new Date(out) : null;
} catch (err) {
console.warn(`[sitemap] shared-mtime git lookup failed (${err?.message ?? err})`);
sharedMtimeCache = null;
}
return sharedMtimeCache;
}
// https://astro.build/config
export default defineConfig({
site: "https://elenkaspanish.com",
vite: {
plugins: [tailwindcss()],
},
integrations: [
sitemap({
serialize(item) {
const pathname = new URL(item.url).pathname;
const file = pageFileForUrl(pathname);
const page = gitMtime(file);
const shared = sharedMtime();
const mtime =
page && shared ? (page > shared ? page : shared) : page || shared;
if (mtime) item.lastmod = mtime.toISOString();
return item;
},
}),
],
});