返回 CodeWhale
layout.tsx
根目录 / web / app / [locale] / layout.tsx
1 import type { Metadata } from "next";
2 import localFont from "next/font/local";
3 import { notFound } from "next/navigation";
4 import { Nav } from "@/components/nav";
5 import { Footer } from "@/components/footer";
6 import { UsageCounting } from "@/components/usage-counting";
7 import { BUILD_FACTS } from "@/lib/facts";
8 import { isValidLocale, localeDirection, locales } from "@/lib/i18n/config";
9 import { getChrome, getHome } from "@/lib/i18n/dictionaries";
10 import { serializeJsonLd } from "@/lib/json-ld";
11 import { buildPageMetadata } from "@/lib/page-meta";
12 import { buildSiteJsonLd } from "@/lib/site-schema";
13 import { metadata as notFoundMetadata } from "./not-found";
14 import "../globals.css";
15
16 const sans = localFont({
17 src: "../../public/brand/fonts/Geist-Variable.woff2",
18 weight: "100 900",
19 style: "normal",
20 variable: "--font-geist-sans",
21 display: "swap",
22 });
23
24 const mono = localFont({
25 src: "../../public/brand/fonts/GeistMono-Variable.woff2",
26 weight: "100 900",
27 style: "normal",
28 variable: "--font-geist-mono",
29 display: "swap",
30 preload: false,
31 });
32
33 export function generateStaticParams() {
34 return locales.map((locale) => ({ locale }));
35 }
36
37 export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }): Promise<Metadata> {
38 const { locale } = await params;
39 // The layout answers not-found for this; without the guard the home page's
40 // title, canonical, and hreflang stream into that 404 (`/wp-login.php`).
41 // Throwing here instead leaves the page with no title at all.
42 if (!isValidLocale(locale)) return notFoundMetadata;
43 const home = getHome(locale);
44 return buildPageMetadata({
45 path: "/",
46 locale,
47 title: home.metaTitle,
48 description: home.metaDescription,
49 });
50 }
51
52 export default async function LocaleLayout({
53 children,
54 params,
55 }: {
56 children: React.ReactNode;
57 params: Promise<{ locale: string }>;
58 }) {
59 const { locale } = await params;
60 // Dotted paths bypass locale redirection so real files keep resolving.
61 // An unknown path such as /foo.txt (or /foo.txt/faq) still binds `[locale]`
62 // here. Reject it before reading dictionaries or rendering home chrome,
63 // so nonexistent files never become HTTP 200 pages with a fake html lang.
64 if (!isValidLocale(locale)) notFound();
65 const chrome = getChrome(locale);
66 // RTL locales (e.g. ar) set the document direction from the canonical
67 // registry so the browser handles bidirectional layout from the root.
68 const dir = localeDirection(locale);
69 const siteJsonLd = buildSiteJsonLd(locale);
70
71 return (
72 <html
73 lang={locale}
74 dir={dir}
75 className={`${sans.variable} ${mono.variable}`}
76 suppressHydrationWarning
77 >
78 <body>
79 <script
80 type="application/ld+json"
81 dangerouslySetInnerHTML={{ __html: serializeJsonLd(siteJsonLd) }}
82 />
83 <a href="#main-content" className="skip-link">
84 {chrome.skipToContent}
85 </a>
86 <Nav locale={locale} />
87 <main id="main-content">{children}</main>
88 <Footer locale={locale} />
89 {/* Aggregate usage counting, on by default — see lib/telemetry. The
90 choice lives on the privacy page; every opt-out stays off. */}
91 <UsageCounting appVersion={BUILD_FACTS.version ?? "0.0.0"} />
92 </body>
93 </html>
94 );
95 }
96
96 lines Plain Text