100 lines
3.3 KiB
TypeScript
100 lines
3.3 KiB
TypeScript
|
|
import type { Metadata } from 'next';
|
|||
|
|
import { getSEOConfig } from '../../../lib/seo-config';
|
|||
|
|
import NewsPageWrapper from '../../components/news/NewsPageWrapper';
|
|||
|
|
|
|||
|
|
// 静态生成时获取文章数据
|
|||
|
|
async function getStaticNewsData(locale: string) {
|
|||
|
|
// 检查是否为静态导出模式
|
|||
|
|
const isStaticMode = process.env.BUILD_MODE === 'static';
|
|||
|
|
|
|||
|
|
if (isStaticMode) {
|
|||
|
|
try {
|
|||
|
|
const { getStaticArticles, getStaticCategories } = await import('../../../lib/static-data');
|
|||
|
|
const mappedLocale = locale === 'zh' ? 'zh-CN' : locale as 'zh-CN' | 'zh-TW' | 'en';
|
|||
|
|
const articles = getStaticArticles(mappedLocale);
|
|||
|
|
const categories = getStaticCategories(mappedLocale);
|
|||
|
|
|
|||
|
|
console.log(`[Static Build] Loaded ${articles.length} articles for locale ${mappedLocale}`);
|
|||
|
|
return { articles, categories };
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('Error loading static data:', error);
|
|||
|
|
return { articles: [], categories: [] };
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 在开发模式或运行时模式,返回null让客户端组件自己获取数据
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Generate static params for supported locales
|
|||
|
|
export async function generateStaticParams() {
|
|||
|
|
return [
|
|||
|
|
{ locale: 'zh' },
|
|||
|
|
{ locale: 'zh-CN' },
|
|||
|
|
{ locale: 'zh-TW' },
|
|||
|
|
{ locale: 'en' },
|
|||
|
|
];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Generate metadata for the news page
|
|||
|
|
export async function generateMetadata({
|
|||
|
|
params: { locale },
|
|||
|
|
}: {
|
|||
|
|
params: { locale: string };
|
|||
|
|
}): Promise<Metadata> {
|
|||
|
|
const language = locale === 'zh' ? 'zh-CN' : locale === 'zh-CN' ? 'zh-CN' : locale === 'zh-TW' ? 'zh-TW' : 'en';
|
|||
|
|
const seo = getSEOConfig('news', language);
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
title: seo.title,
|
|||
|
|
description: seo.description,
|
|||
|
|
keywords: seo.keywords,
|
|||
|
|
openGraph: {
|
|||
|
|
title: seo.openGraph?.title,
|
|||
|
|
description: seo.openGraph?.description,
|
|||
|
|
type: seo.openGraph?.type as any,
|
|||
|
|
images: seo.openGraph?.images,
|
|||
|
|
},
|
|||
|
|
twitter: {
|
|||
|
|
card: seo.twitter?.card as any,
|
|||
|
|
title: seo.twitter?.title,
|
|||
|
|
description: seo.twitter?.description,
|
|||
|
|
images: seo.twitter?.images,
|
|||
|
|
},
|
|||
|
|
alternates: {
|
|||
|
|
canonical: locale === 'zh' ? '/news' : `/${locale}/news`,
|
|||
|
|
languages: {
|
|||
|
|
'zh-CN': '/news',
|
|||
|
|
'zh-TW': '/zh-TW/news',
|
|||
|
|
en: '/en/news',
|
|||
|
|
},
|
|||
|
|
},
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export default async function NewsPage({ params: { locale } }: { params: { locale: string } }) {
|
|||
|
|
// 获取静态数据(如果是静态模式)
|
|||
|
|
const staticData = await getStaticNewsData(locale);
|
|||
|
|
|
|||
|
|
// 在静态模式下,确保数据被正确传递
|
|||
|
|
if (staticData) {
|
|||
|
|
console.log(`[NewsPage] Passing ${staticData.articles.length} articles to wrapper component`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return (
|
|||
|
|
<>
|
|||
|
|
{/* 将数据序列化为JSON并嵌入页面,供客户端组件使用 */}
|
|||
|
|
{staticData && (
|
|||
|
|
<script
|
|||
|
|
id="initial-news-data"
|
|||
|
|
type="application/json"
|
|||
|
|
dangerouslySetInnerHTML={{
|
|||
|
|
__html: JSON.stringify(staticData)
|
|||
|
|
}}
|
|||
|
|
/>
|
|||
|
|
)}
|
|||
|
|
<NewsPageWrapper locale={locale} initialData={staticData} />
|
|||
|
|
</>
|
|||
|
|
);
|
|||
|
|
}
|