18e915bcbb
- 文章管理:计数请求补 page=1 参数,命中分页接口返回正确的 total - 分类管理:编辑模式新增描述输入框,保存时一并提交 description - CSP:img-src 加入 https: 允许加载外部图片 - 关于页:数据存储描述从 JSON 文件更正为 SQLite 数据库 - Footer:添加 ICP 备案号
77 lines
2.3 KiB
TypeScript
77 lines
2.3 KiB
TypeScript
import type { Metadata } from "next";
|
|
import { notFound } from "next/navigation";
|
|
import { getPostBySlug, getPublishedPosts } from "@/lib/store";
|
|
import PostContent from "@/components/PostContent";
|
|
|
|
const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || "https://asui.xyz";
|
|
|
|
export async function generateStaticParams() {
|
|
const posts = await getPublishedPosts();
|
|
return posts.map((post) => ({ slug: post.slug }));
|
|
}
|
|
|
|
export async function generateMetadata({
|
|
params,
|
|
}: {
|
|
params: Promise<{ slug: string }>;
|
|
}): Promise<Metadata> {
|
|
const { slug } = await params;
|
|
const post = await getPostBySlug(slug);
|
|
if (!post) return {};
|
|
|
|
const url = `${SITE_URL}/posts/${post.slug}`;
|
|
return {
|
|
title: post.title,
|
|
description: post.excerpt,
|
|
alternates: { canonical: url },
|
|
openGraph: {
|
|
title: post.title,
|
|
description: post.excerpt,
|
|
url,
|
|
type: "article",
|
|
publishedTime: post.date,
|
|
authors: ["胡旭"],
|
|
tags: post.tags,
|
|
siteName: "随 · asui.xyz",
|
|
locale: "zh_CN",
|
|
},
|
|
twitter: {
|
|
card: "summary_large_image",
|
|
title: post.title,
|
|
description: post.excerpt,
|
|
},
|
|
};
|
|
}
|
|
|
|
export default async function PostPage({ params }: { params: Promise<{ slug: string }> }) {
|
|
const { slug } = await params;
|
|
const post = await getPostBySlug(slug);
|
|
if (!post) notFound();
|
|
|
|
const all = await getPublishedPosts();
|
|
const currentIndex = all.findIndex((p) => p.slug === slug);
|
|
const prevPost = currentIndex > 0 ? all[currentIndex - 1] : null;
|
|
const nextPost = currentIndex < all.length - 1 ? all[currentIndex + 1] : null;
|
|
|
|
// BlogPosting 结构化数据,利于搜索引擎富摘要
|
|
const jsonLd = {
|
|
"@context": "https://schema.org",
|
|
"@type": "BlogPosting",
|
|
headline: post.title,
|
|
description: post.excerpt,
|
|
datePublished: post.date,
|
|
dateModified: post.updatedAt,
|
|
author: { "@type": "Person", name: "胡旭", url: SITE_URL },
|
|
publisher: { "@type": "Person", name: "胡旭" },
|
|
mainEntityOfPage: `${SITE_URL}/posts/${post.slug}`,
|
|
keywords: post.tags.join(", "),
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<script type="application/ld+json" dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }} />
|
|
<PostContent post={post} prevPost={prevPost} nextPost={nextPost} />
|
|
</>
|
|
);
|
|
}
|