From 38f2a9823bf6f9affaf877cbcc00a2c2b6325b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E6=97=AD?= <> Date: Wed, 24 Jun 2026 17:20:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20sitemap=20=E5=8A=A0=20force-dynamic=20+?= =?UTF-8?q?=20try-catch=EF=BC=88=E6=9E=84=E5=BB=BA=E6=97=B6=E4=B8=8D?= =?UTF-8?q?=E6=9F=A5=E6=95=B0=E6=8D=AE=E5=BA=93=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/sitemap.ts | 52 ++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/src/app/sitemap.ts b/src/app/sitemap.ts index da45d2f..128d4b7 100644 --- a/src/app/sitemap.ts +++ b/src/app/sitemap.ts @@ -1,6 +1,8 @@ import type { MetadataRoute } from "next"; import { getPublishedPosts, getPublicCategories, getAllTags } from "@/lib/store"; +export const dynamic = "force-dynamic"; + const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || "https://asui.xyz"; export default async function sitemap(): Promise { @@ -12,32 +14,36 @@ export default async function sitemap(): Promise { { url: `${SITE_URL}/about`, changeFrequency: "yearly", priority: 0.5 }, ]; - const [publishedPosts, tags, categories] = await Promise.all([ - getPublishedPosts(), - getAllTags(), - getPublicCategories(), - ]); + try { + const [publishedPosts, tags, categories] = await Promise.all([ + getPublishedPosts(), + getAllTags(), + getPublicCategories(), + ]); - const postRoutes: MetadataRoute.Sitemap = publishedPosts.map((post) => ({ - url: `${SITE_URL}/posts/${post.slug}`, - lastModified: new Date(post.updatedAt || post.date), - changeFrequency: "monthly", - priority: 0.8, - })); + const postRoutes: MetadataRoute.Sitemap = publishedPosts.map((post) => ({ + url: `${SITE_URL}/posts/${post.slug}`, + lastModified: new Date(post.updatedAt || post.date), + changeFrequency: "monthly", + priority: 0.8, + })); - const tagRoutes: MetadataRoute.Sitemap = tags.map((tag) => ({ - url: `${SITE_URL}/blog?tag=${encodeURIComponent(tag.name)}`, - 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, + const tagRoutes: MetadataRoute.Sitemap = tags.map((tag) => ({ + url: `${SITE_URL}/blog?tag=${encodeURIComponent(tag.name)}`, + changeFrequency: "monthly", 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; + } }