Skip to content

Commit 9b048dc

Browse files
authored
Merge pull request #198 from ut-code/add-sitemap
sitemap, robots.txt, commits.ymlを追加
2 parents fc88869 + 7f9403b commit 9b048dc

5 files changed

Lines changed: 115 additions & 0 deletions

File tree

app/lib/docs.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ export interface RevisionYmlEntry {
9595
* `langId/pageSlug`
9696
*/
9797
page: string;
98+
/**
99+
* revisionのリストは末尾が最新のはず。
100+
*/
98101
rev: SectionRevision[];
99102
}
100103
export interface SectionRevision {
@@ -184,6 +187,25 @@ export async function getRevisions(
184187
];
185188
}
186189

190+
const commitDateCache = new Map<string, Promise<Date>>();
191+
export async function getCommitDate(id: string): Promise<Date> {
192+
if (commitDateCache.has(id)) {
193+
return commitDateCache.get(id)!;
194+
}
195+
const p = (async () => {
196+
const commitInfoYml = await readPublicFile(`docs/commits.yml`);
197+
const commitInfo = yaml.load(commitInfoYml) as Record<string, string>;
198+
const timestamp = commitInfo[id];
199+
if (timestamp) {
200+
return new Date(timestamp);
201+
} else {
202+
throw new Error(`Commit date for id=${id} not found`);
203+
}
204+
})();
205+
commitDateCache.set(id, p);
206+
return p;
207+
}
208+
187209
/**
188210
* public/docs/{lang}/{pageId}/ 以下のmdファイルを結合して MarkdownSection[] を返す。
189211
*/

app/robots.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { MetadataRoute } from "next";
2+
3+
export const dynamic = "force-static";
4+
5+
const origin = "https://my-code.utcode.net";
6+
7+
export default function robots(): MetadataRoute.Robots {
8+
return {
9+
rules: {
10+
userAgent: "*",
11+
allow: "/",
12+
},
13+
sitemap: `${origin}/sitemap.xml`,
14+
};
15+
}

app/sitemap.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { MetadataRoute } from "next";
2+
import {
3+
getCommitDate,
4+
getMarkdownSections,
5+
getPagesList,
6+
getRevisions,
7+
} from "./lib/docs";
8+
9+
export const dynamic = "force-static";
10+
11+
const origin = "https://my-code.utcode.net";
12+
13+
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
14+
const pagesList = await getPagesList();
15+
16+
return [
17+
{
18+
url: origin,
19+
changeFrequency: "monthly",
20+
priority: 1,
21+
},
22+
...(await Promise.all(
23+
pagesList
24+
.map((lang) =>
25+
lang.pages.map(async (page) => {
26+
const sections = await getMarkdownSections(lang.id, page.slug);
27+
const sectionsDate = await Promise.all(
28+
sections.map((s) =>
29+
getRevisions(s.id)
30+
.then((revisions) => revisions?.rev.at(-1))
31+
.then((lastRev) =>
32+
lastRev ? getCommitDate(lastRev.git) : null
33+
)
34+
)
35+
);
36+
return {
37+
url: `${origin}/${lang.id}/${page.slug}`,
38+
priority: 0.8,
39+
changeFrequency: "monthly",
40+
lastModified: sectionsDate.reduce(
41+
(latest: Date, date: Date | null) => {
42+
if (!date) return latest;
43+
return date > latest ? date : latest;
44+
},
45+
new Date(0)
46+
),
47+
} satisfies MetadataRoute.Sitemap[number];
48+
})
49+
)
50+
.flat()
51+
)),
52+
];
53+
}

public/docs/commits.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This file will be updated by CI. Do not edit manually.
2+
de11512: '2026-03-08T14:27:20+09:00'

scripts/checkDocs.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ if (doWrite) {
5050
}).trim();
5151
}
5252

53+
let hasNewRevision = false;
54+
5355
const langEntries = await getPagesList();
5456

5557
const revisionsPrevYml = existsSync(join(docsDir, "revisions.yml"))
@@ -70,6 +72,7 @@ for (const lang of langEntries) {
7072
// ドキュメントが変更された場合
7173
console.warn(`${section.id} has new md5: ${section.md5}`);
7274
if (doWrite) {
75+
hasNewRevision = true;
7376
revisions[section.id].rev.push({
7477
md5: section.md5,
7578
git: commit,
@@ -83,6 +86,7 @@ for (const lang of langEntries) {
8386
// ドキュメントが新規追加された場合
8487
console.warn(`${section.id} is new section`);
8588
if (doWrite) {
89+
hasNewRevision = true;
8690
revisions[section.id] = {
8791
rev: [
8892
{
@@ -142,4 +146,23 @@ if (doWrite) {
142146
revisionsYml,
143147
"utf-8"
144148
);
149+
150+
if (hasNewRevision) {
151+
const commitsYml = yaml.load(
152+
await readFile(join(docsDir, "commits.yml"), "utf-8")
153+
) as Record<string, string>;
154+
commitsYml[commit] = execFileSync(
155+
"git",
156+
["show", "-s", "--format=%cI", commit],
157+
{
158+
encoding: "utf8",
159+
}
160+
).trim();
161+
await writeFile(
162+
join(docsDir, "commits.yml"),
163+
"# This file will be updated by CI. Do not edit manually.\n" +
164+
yaml.dump(commitsYml, { sortKeys: true }),
165+
"utf-8"
166+
);
167+
}
145168
}

0 commit comments

Comments
 (0)