import type { Metadata } from "next"; import { cookies } from "next/headers"; import { fetchSnippets, fetchSnippetLanguages, fetchSnippetStats, type SnippetStats } from "@/lib/api"; import { getMeCached, getPublicSettingsCached } from "@/lib/serverData"; import { authCookieHeader } from "@/lib/cookies"; import { requestCanonical } from "@/lib/canonicalMeta"; import { snippetsPath } from "@/lib/urlStyle"; import SnippetExplorer from "@/components/SnippetExplorer"; export async function generateMetadata(): Promise { const settings = await getPublicSettingsCached(); return { title: "代码片段", description: settings.site_description, ...(await requestCanonical(snippetsPath(settings.url_style))), }; } export default async function SnippetsPage() { const settings = await getPublicSettingsCached(); // 登录态由服务端判定后下发:客户端组件内再读 cookie 会造成 hydration 不一致。 // getMeCached 与 layout 同参调用,命中 React cache 不额外请求后端。 const cookie = authCookieHeader(await cookies()); const isLoggedIn = !!(await getMeCached(cookie || undefined)).user; let initialItems: Awaited>["items"] = []; let initialTotal = 0; let languages: string[] = []; let stats: SnippetStats | null = null; try { const [listRes, langRes, statsRes] = await Promise.all([ fetchSnippets({ size: 12 }), fetchSnippetLanguages(), fetchSnippetStats(), ]); initialItems = listRes.items; initialTotal = listRes.total; languages = langRes.languages; stats = statsRes; } catch { /* 降级空态 */ } return (
); }