Files
PolyWeather/frontend/app/methodology/[slug]/page.tsx
T

119 lines
3.1 KiB
TypeScript
Raw Normal View History

2026-06-24 16:41:13 +08:00
import type { Metadata } from "next";
2026-06-24 18:53:22 +08:00
import { cookies, headers } from "next/headers";
2026-06-24 16:41:13 +08:00
import { notFound } from "next/navigation";
import { MethodologyDetailPageView } from "@/components/public-content/PublicContentPages";
2026-06-24 18:53:22 +08:00
import {
LANDING_LOCALE_COOKIE,
LANDING_LOCALE_QUERY_PARAM,
pickLandingLocale,
type LandingLocale,
} from "@/components/landing/landingLocale";
2026-06-24 16:41:13 +08:00
import {
METHODOLOGY_PAGES,
absolutePublicUrl,
getMethodologyPage,
2026-06-24 18:53:22 +08:00
localizeMethodologyPage,
2026-06-24 16:41:13 +08:00
methodologyPath,
} from "@/content/public-content";
type MethodologyPageParams = {
slug: string;
};
2026-06-24 18:53:22 +08:00
type MethodologySearchParams = Promise<Record<string, string | string[] | undefined>>;
async function resolvePublicContentLocale(searchParams: MethodologySearchParams): Promise<LandingLocale> {
const params = await searchParams;
const rawLocale = params[LANDING_LOCALE_QUERY_PARAM];
const queryLocale = Array.isArray(rawLocale) ? rawLocale[0] : rawLocale;
const [cookieStore, headerStore] = await Promise.all([cookies(), headers()]);
return pickLandingLocale(
queryLocale,
cookieStore.get(LANDING_LOCALE_COOKIE)?.value,
headerStore.get("accept-language"),
);
}
2026-06-24 16:41:13 +08:00
export function generateStaticParams() {
return METHODOLOGY_PAGES.map((page) => ({ slug: page.slug }));
}
export async function generateMetadata({
params,
2026-06-24 18:53:22 +08:00
searchParams,
2026-06-24 16:41:13 +08:00
}: {
params: Promise<MethodologyPageParams>;
2026-06-24 18:53:22 +08:00
searchParams: MethodologySearchParams;
2026-06-24 16:41:13 +08:00
}): Promise<Metadata> {
2026-06-24 18:53:22 +08:00
const [{ slug }, locale] = await Promise.all([
params,
resolvePublicContentLocale(searchParams),
]);
2026-06-24 16:41:13 +08:00
const page = getMethodologyPage(slug);
if (!page) {
return {
title: "Methodology not found",
};
}
2026-06-24 18:53:22 +08:00
const localizedPage = localizeMethodologyPage(page, locale);
2026-06-24 16:41:13 +08:00
return {
2026-06-24 18:53:22 +08:00
title: localizedPage.title,
description: localizedPage.description,
2026-06-24 16:41:13 +08:00
alternates: {
canonical: methodologyPath(page),
},
openGraph: {
type: "article",
2026-06-24 18:53:22 +08:00
title: localizedPage.title,
description: localizedPage.description,
2026-06-24 16:41:13 +08:00
url: methodologyPath(page),
modifiedTime: page.updatedAt,
},
};
}
export default async function MethodologyDetailPage({
params,
2026-06-24 18:53:22 +08:00
searchParams,
2026-06-24 16:41:13 +08:00
}: {
params: Promise<MethodologyPageParams>;
2026-06-24 18:53:22 +08:00
searchParams: MethodologySearchParams;
2026-06-24 16:41:13 +08:00
}) {
2026-06-24 18:53:22 +08:00
const [{ slug }, locale] = await Promise.all([
params,
resolvePublicContentLocale(searchParams),
]);
2026-06-24 16:41:13 +08:00
const page = getMethodologyPage(slug);
if (!page) {
notFound();
}
2026-06-24 18:53:22 +08:00
const localizedPage = localizeMethodologyPage(page, locale);
2026-06-24 16:41:13 +08:00
const pathname = methodologyPath(page);
const jsonLd = {
"@context": "https://schema.org",
"@type": "TechArticle",
2026-06-24 18:53:22 +08:00
headline: localizedPage.title,
description: localizedPage.description,
2026-06-24 16:41:13 +08:00
dateModified: page.updatedAt,
mainEntityOfPage: absolutePublicUrl(pathname),
author: {
"@type": "Organization",
name: "PolyWeather",
url: "https://polyweather.top",
},
};
return (
<>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
/>
2026-06-24 18:53:22 +08:00
<MethodologyDetailPageView page={page} locale={locale} />
2026-06-24 16:41:13 +08:00
</>
);
}