fix: sitemap 加 force-dynamic + try-catch(构建时不查数据库)
This commit is contained in:
+29
-23
@@ -1,6 +1,8 @@
|
|||||||
import type { MetadataRoute } from "next";
|
import type { MetadataRoute } from "next";
|
||||||
import { getPublishedPosts, getPublicCategories, getAllTags } from "@/lib/store";
|
import { getPublishedPosts, getPublicCategories, getAllTags } from "@/lib/store";
|
||||||
|
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || "https://asui.xyz";
|
const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || "https://asui.xyz";
|
||||||
|
|
||||||
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
||||||
@@ -12,32 +14,36 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
|||||||
{ url: `${SITE_URL}/about`, changeFrequency: "yearly", priority: 0.5 },
|
{ url: `${SITE_URL}/about`, changeFrequency: "yearly", priority: 0.5 },
|
||||||
];
|
];
|
||||||
|
|
||||||
const [publishedPosts, tags, categories] = await Promise.all([
|
try {
|
||||||
getPublishedPosts(),
|
const [publishedPosts, tags, categories] = await Promise.all([
|
||||||
getAllTags(),
|
getPublishedPosts(),
|
||||||
getPublicCategories(),
|
getAllTags(),
|
||||||
]);
|
getPublicCategories(),
|
||||||
|
]);
|
||||||
|
|
||||||
const postRoutes: MetadataRoute.Sitemap = publishedPosts.map((post) => ({
|
const postRoutes: MetadataRoute.Sitemap = publishedPosts.map((post) => ({
|
||||||
url: `${SITE_URL}/posts/${post.slug}`,
|
url: `${SITE_URL}/posts/${post.slug}`,
|
||||||
lastModified: new Date(post.updatedAt || post.date),
|
lastModified: new Date(post.updatedAt || post.date),
|
||||||
changeFrequency: "monthly",
|
changeFrequency: "monthly",
|
||||||
priority: 0.8,
|
priority: 0.8,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const tagRoutes: MetadataRoute.Sitemap = tags.map((tag) => ({
|
const tagRoutes: MetadataRoute.Sitemap = tags.map((tag) => ({
|
||||||
url: `${SITE_URL}/blog?tag=${encodeURIComponent(tag.name)}`,
|
url: `${SITE_URL}/blog?tag=${encodeURIComponent(tag.name)}`,
|
||||||
changeFrequency: "monthly",
|
changeFrequency: "monthly",
|
||||||
priority: 0.4,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const categoryRoutes: MetadataRoute.Sitemap = categories
|
|
||||||
.filter((c) => c.count > 0)
|
|
||||||
.map((cat) => ({
|
|
||||||
url: `${SITE_URL}/blog?category=${encodeURIComponent(cat.name)}`,
|
|
||||||
changeFrequency: "monthly" as const,
|
|
||||||
priority: 0.4,
|
priority: 0.4,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return [...staticRoutes, ...postRoutes, ...tagRoutes, ...categoryRoutes];
|
const categoryRoutes: MetadataRoute.Sitemap = categories
|
||||||
|
.filter((c) => c.count > 0)
|
||||||
|
.map((cat) => ({
|
||||||
|
url: `${SITE_URL}/blog?category=${encodeURIComponent(cat.name)}`,
|
||||||
|
changeFrequency: "monthly" as const,
|
||||||
|
priority: 0.4,
|
||||||
|
}));
|
||||||
|
|
||||||
|
return [...staticRoutes, ...postRoutes, ...tagRoutes, ...categoryRoutes];
|
||||||
|
} catch {
|
||||||
|
return staticRoutes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user