Files
web_hghl_h5/src/app/page.tsx
T
2026-10-26 19:51:59 +07:00

94 lines
3.3 KiB
TypeScript

import type { Metadata } from "next";
import { cache } from "react";
import { HomeDataDebug } from "@/components/home/home-data-debug";
import { HomePage } from "@/components/home/home-page";
import { JsonLd } from "@/components/seo/json-ld";
import { dramaHref } from "@/components/short-drama/data";
import { getHomeBaseCached } from "@/lib/api/home";
import { getPlayletHomeCached } from "@/lib/api/playlet";
import type { AdvitiseData, ConfigInfo, HomePlayletResponse, PlayletData } from "@/lib/api/types";
import {
buildWebPageJSONLD,
getHomeBaseSEOCached,
mergeSEO,
resolvePageMetadata,
} from "@/lib/seo";
function summarizeFetchError(error: unknown) {
if (!(error instanceof Error)) return String(error);
const cause = (error as Error & { cause?: { code?: string; message?: string } }).cause;
const code = cause?.code || (error as Error & { code?: string }).code;
if (code) return `${error.message} (${code})`;
return error.message;
}
/** 同请求内 metadata + page 共用一次结果 */
const loadHome = cache(async (): Promise<HomePlayletResponse | null> => {
try {
return await getPlayletHomeCached();
} catch (error) {
console.warn(`[playlet/home] ${summarizeFetchError(error)}; fallback to local mock`);
return null;
}
});
const loadHomeBase = cache(async (): Promise<{ advitise?: AdvitiseData; config?: ConfigInfo }> => {
try {
const base = await getHomeBaseCached();
return { advitise: base.advitise, config: base.config };
} catch (error) {
console.warn(`[home/base] ${summarizeFetchError(error)}`);
return {};
}
});
export async function generateMetadata(): Promise<Metadata> {
const home = await loadHome();
// seoInfo → home/base 公共 web* → 代码兜底
return resolvePageMetadata({
seoInfo: home?.seoInfo,
fallbackTitle: "黄果黑料 - 热门短剧在线看",
fallbackDescription: "黄果黑料汇集热门短剧、最近上新、排行榜与精选专题推荐,支持高清在线播放。",
fallbackKeywords: ["黄果黑料", "短剧", "在线短剧", "热门短剧"],
canonical: "/",
});
}
export default async function Home() {
const [home, base, siteSeo] = await Promise.all([loadHome(), loadHomeBase(), getHomeBaseSEOCached()]);
const merged = home
? { ...home, advitise: base.advitise ?? home.advitise }
: base.advitise
? { advitise: base.advitise }
: null;
const seo = mergeSEO(home?.seoInfo, siteSeo.seo);
const homeTitle = seo.title || "黄果黑料 - 热门短剧在线看";
const homeDescription =
seo.description || "黄果黑料汇集热门短剧、最近上新、排行榜与精选专题推荐,支持高清在线播放。";
const itemListUrls =
home?.homeBannerData
?.filter((item): item is PlayletData & { id: number } => Boolean(item?.id))
.slice(0, 12)
.map((item) => dramaHref(item.id, item.slug)) ?? [];
return (
<>
<HomeDataDebug home={merged} base={base} />
<JsonLd
data={buildWebPageJSONLD({
path: "/",
title: homeTitle,
description: homeDescription,
type: "CollectionPage",
breadcrumbs: [["首页", "/"]],
itemListUrls,
siteName: siteSeo.siteName,
})}
/>
<HomePage home={merged} heading={homeTitle} />
</>
);
}