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
| // src/app/sitemap.ts
import { getCmsEntriesForSitemap } from "@/lib/cms/cms-repository";
import { SITE_URL } from "@/constants";
import { withKVCache, CACHE_KEYS } from "@/utils/with-kv-cache";
export async function GET() {
const sitemap = await withKVCache(
async () => {
const entries = await getCmsEntriesForSitemap();
const urls = entries.map((entry) => ({
url: `${SITE_URL}/${entry.collection}/${entry.slug}`,
lastModified: entry.updatedAt || entry.createdAt,
changeFrequency: entry.collection === "blog" ? "weekly" : "monthly",
priority: entry.collection === "docs" ? 0.9 : 0.7,
}));
// 静态页面
urls.push(
{ url: SITE_URL, lastModified: new Date(), changeFrequency: "daily", priority: 1.0 },
{ url: `${SITE_URL}/blog`, lastModified: new Date(), changeFrequency: "daily", priority: 0.8 },
{ url: `${SITE_URL}/docs`, lastModified: new Date(), changeFrequency: "weekly", priority: 0.9 },
);
return urls;
},
{ key: CACHE_KEYS.SITEMAP, ttl: "1h" }
);
// 生成 XML
const xml = generateSitemapXml(sitemap);
return new Response(xml, {
headers: {
"Content-Type": "application/xml",
"Cache-Control": "public, max-age=3600, s-maxage=3600",
},
});
}
|