feat(article): 添加静态参数生成以优化文章页面加载速度

- 引入 getArticlePagingCacheAPI 以提前下载前80篇文章,提升页面加载性能。
- 更新 generateStaticParams 函数,确保在获取文章数据时处理异常情况。
This commit is contained in:
刘宇阳
2026-07-08 14:28:31 +08:00
parent 595f0afabe
commit 8b00dcaa17

View File

@@ -1,6 +1,6 @@
import { getArticleDataAPI, recordViewAPI } from '@/api/article';
import { getWebConfigCacheAPI } from '@/lib/config';
import { getArticleCacheAPI } from '@/lib/article';
import { getArticleCacheAPI, getArticlePagingCacheAPI } from '@/lib/article';
import { getThemeConfigCacheAPI } from '@/lib/theme';
import { getStableImage } from '@/utils';
import { Metadata } from 'next';
@@ -35,6 +35,18 @@ interface Props {
searchParams: Promise<{ password: string }>;
}
// 提前下载好前80篇文章提高页面加载速度
export async function generateStaticParams() {
try {
const { data } = await getArticlePagingCacheAPI({ pageNum: 1, pageSize: 80 });
return (data?.result ?? [])
.filter((article) => article.id != null)
.map((article) => ({ id: String(article.id) }));
} catch {
return [];
}
}
// 生成文章页面的 metadata
export async function generateMetadata(props: Props): Promise<Metadata> {
const params = await props.params;