131 lines
3.3 KiB
TypeScript
131 lines
3.3 KiB
TypeScript
import type { MetadataRoute } from "next";
|
|
import { connection } from "next/server";
|
|
import {
|
|
fetchPosts,
|
|
fetchAnnouncementsPage,
|
|
fetchPublishedSitePages,
|
|
fetchLibraryList,
|
|
fetchSnippets,
|
|
} from "@/lib/api";
|
|
import { canonicalPath, resolveSiteOrigin } from "@/lib/siteUrl";
|
|
import { announcementPath, postPath, pagePath, leaderboardPath, libraryPath, libraryDocPath, snippetsPath, snippetPath } from "@/lib/urlStyle";
|
|
import { getPublicSettingsCached } from "@/lib/serverData";
|
|
|
|
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
|
await connection();
|
|
const origin = resolveSiteOrigin();
|
|
if (!origin) {
|
|
console.error("[seo] SITE_URL 缺失或非法,已跳过 sitemap");
|
|
return [];
|
|
}
|
|
const style = (await getPublicSettingsCached()).url_style;
|
|
|
|
const url = (pathname: string) => `${origin}${canonicalPath(pathname)}`;
|
|
const entries: MetadataRoute.Sitemap = [
|
|
{ url: url("/"), lastModified: new Date(), changeFrequency: "daily", priority: 1 },
|
|
{
|
|
url: url("/announcements"),
|
|
lastModified: new Date(),
|
|
changeFrequency: "daily",
|
|
priority: 0.7,
|
|
},
|
|
{
|
|
url: url(leaderboardPath(style)),
|
|
lastModified: new Date(),
|
|
changeFrequency: "daily",
|
|
priority: 0.5,
|
|
},
|
|
{
|
|
url: url(libraryPath(style)),
|
|
lastModified: new Date(),
|
|
changeFrequency: "daily",
|
|
priority: 0.5,
|
|
},
|
|
{
|
|
url: url(snippetsPath(style)),
|
|
lastModified: new Date(),
|
|
changeFrequency: "daily",
|
|
priority: 0.5,
|
|
},
|
|
{
|
|
url: url("/about"),
|
|
lastModified: new Date(),
|
|
changeFrequency: "monthly",
|
|
priority: 0.6,
|
|
},
|
|
];
|
|
|
|
try {
|
|
const { posts } = await fetchPosts(1, 100);
|
|
for (const post of posts) {
|
|
entries.push({
|
|
url: url(postPath(style, post.id)),
|
|
lastModified: new Date(post.created_at),
|
|
changeFrequency: "weekly",
|
|
priority: 0.6,
|
|
});
|
|
}
|
|
} catch {
|
|
/* 忽略 API 错误 */
|
|
}
|
|
|
|
try {
|
|
const { announcements } = await fetchAnnouncementsPage(1, 100);
|
|
for (const a of announcements) {
|
|
entries.push({
|
|
url: url(announcementPath(style, a.id)),
|
|
lastModified: new Date(a.updated_at || a.created_at),
|
|
changeFrequency: "weekly",
|
|
priority: 0.5,
|
|
});
|
|
}
|
|
} catch {
|
|
/* 忽略 */
|
|
}
|
|
|
|
try {
|
|
const { pages } = await fetchPublishedSitePages();
|
|
for (const p of pages) {
|
|
entries.push({
|
|
url: url(pagePath(style, p.slug)),
|
|
lastModified: new Date(p.updated_at || p.created_at),
|
|
changeFrequency: "monthly",
|
|
priority: 0.4,
|
|
});
|
|
}
|
|
} catch {
|
|
/* 忽略 */
|
|
}
|
|
|
|
try {
|
|
const { docs } = await fetchLibraryList();
|
|
for (const d of docs) {
|
|
entries.push({
|
|
url: url(libraryDocPath(style, d.slug)),
|
|
lastModified: new Date(d.updated_at || d.created_at),
|
|
changeFrequency: "weekly",
|
|
priority: 0.4,
|
|
});
|
|
}
|
|
} catch {
|
|
/* 忽略 */
|
|
}
|
|
|
|
try {
|
|
const { items } = await fetchSnippets({ page: 1, size: 200 });
|
|
for (const it of items) {
|
|
if (it.visibility !== "public") continue;
|
|
entries.push({
|
|
url: url(snippetPath(style, it.id)),
|
|
lastModified: new Date(it.updated_at || it.created_at),
|
|
changeFrequency: "weekly",
|
|
priority: 0.4,
|
|
});
|
|
}
|
|
} catch {
|
|
/* 忽略 */
|
|
}
|
|
|
|
return entries;
|
|
}
|