mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
perf(i18n): stop shipping two full catalogs on every page load
Two independent every-load costs, one per layer: 1. The static EN import in load-locale.ts put the full en.json catalog (334 KB raw / ~106 KB gz) into the client JS of EVERY route, for every user in every language. No catalog is statically imported by client code anymore: the EN fallback floor of the t() chain loads lazily on the first actual key miss (the locale-integrity guard keeps every locale key-complete, so the miss path is drift-only), and the labs biomarker canonical-name lookup fetches EN on demand. 2. The root layout serialized the ENTIRE active-locale catalog into the RSC flight payload of every document (392 KB of a 505 KB dashboard HTML, re-downloaded on every hard load, uncacheable). The handoff is now a versioned, immutable-cacheable boot script (/i18n/<locale>?v=<build>, new route handler) that assigns self.__HL_I18N before the hydration scripts run, plus a server-side cache seed in load-locale.ts behind a typeof-window branch that Turbopack compiles out of the browser bundle (verified: zero catalog bytes in any client chunk, zero route manifests referencing one). SSR text stays correct and the first client render holds the same bundle the server rendered with — no EN flash, no hydration drift. The boot script is public (proxy allowlist: the login page needs it pre-auth; the body is the same catalog JSON that ships in the public repo) and cache-first in the service worker, so an offline relaunch finds it next to the cached shell. Measured (client-reference manifests, gzip): route JS baseline / 391->287 KB gz, /insights 377->273, /measurements 379->275, /insights/mood 394->290 — the ~104 KB gz EN catalog left every route. H2
This commit is contained in:
1 parent
e260b2d5c4
commit
e24db8a889
8 files changed
+232
-61
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import { locales, type Locale } from "@/lib/i18n/config";
|
||||
import { allMessages } from "@/lib/i18n/shared-resolve";
|
||||
|
||||
/**
|
||||
* Locale-catalog boot script.
|
||||
*
|
||||
* The root layout emits `<script src="/i18n/<locale>?v=<version>" defer>`
|
||||
* ahead of Next's hydration scripts. The body assigns the active locale's
|
||||
* full message bundle to `self.__HL_I18N`, which `load-locale.ts` reads in
|
||||
* its browser cache initializer — so the first client render holds the same
|
||||
* strings the server rendered with.
|
||||
*
|
||||
* This replaces the former RSC-prop handoff that serialized the ENTIRE
|
||||
* active catalog into every document's flight payload (392 KB of a 505 KB
|
||||
* dashboard HTML, re-downloaded on every hard load). As a versioned URL the
|
||||
* catalog is immutable-cacheable: one download per deploy per locale, then
|
||||
* HTTP/SW cache. The `?v=` query is only a cache key; the handler always
|
||||
* serves the catalog baked into the running build.
|
||||
*
|
||||
* Public by design (allowlisted in `src/proxy.ts`): the login page needs it
|
||||
* pre-auth, and the catalogs are the same strings that ship in the public
|
||||
* repository — no tenant data, no secrets.
|
||||
*/
|
||||
|
||||
const IMMUTABLE_CACHE = "public, max-age=31536000, immutable";
|
||||
|
||||
export async function GET(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ locale: string }> },
|
||||
) {
|
||||
const { locale } = await params;
|
||||
if (!(locales as readonly string[]).includes(locale)) {
|
||||
return new NextResponse(null, { status: 404 });
|
||||
}
|
||||
|
||||
const payload = JSON.stringify({
|
||||
locale,
|
||||
messages: allMessages[locale as Locale],
|
||||
});
|
||||
return new NextResponse(`self.__HL_I18N = ${payload};`, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "text/javascript; charset=utf-8",
|
||||
"Cache-Control": IMMUTABLE_CACHE,
|
||||
// Belt and suspenders for a script-typed response.
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
},
|
||||
});
|
||||
}
|
||||
+18
-12
@@ -7,7 +7,6 @@ import { AuthShell } from "@/components/layout/auth-shell";
|
||||
import { MonitoringBootstrap } from "@/components/monitoring/bootstrap";
|
||||
import { WebVitalsReporter } from "@/components/monitoring/web-vitals-reporter";
|
||||
import { resolveInitialLocale } from "@/lib/i18n/resolve-initial-locale";
|
||||
import { allMessages } from "@/lib/i18n/shared-resolve";
|
||||
|
||||
const inter = Inter({
|
||||
variable: "--font-sans",
|
||||
@@ -89,13 +88,6 @@ export default async function RootLayout({
|
||||
: undefined;
|
||||
|
||||
const initialLocale = await resolveInitialLocale();
|
||||
// RSC handoff for the i18n bundle split: only EN ships statically in
|
||||
// the client chunk, so a non-EN first paint needs its bundle inlined
|
||||
// into the payload here — that is what keeps the split free of the
|
||||
// EN→DE hydration flash. EN passes nothing (the client already holds
|
||||
// the static fallback floor).
|
||||
const initialMessages =
|
||||
initialLocale === "en" ? undefined : allMessages[initialLocale];
|
||||
|
||||
return (
|
||||
<html lang={initialLocale} suppressHydrationWarning>
|
||||
@@ -105,12 +97,26 @@ export default async function RootLayout({
|
||||
nonce={nonce}
|
||||
dangerouslySetInnerHTML={{ __html: themeScript }}
|
||||
/>
|
||||
{/*
|
||||
Locale-catalog boot script (see src/app/i18n/[locale]/route.ts).
|
||||
Replaces the former RSC-prop handoff that inlined the whole
|
||||
active catalog into every document's flight payload (392 KB of a
|
||||
505 KB dashboard HTML). `defer` keeps it off the parse/paint
|
||||
critical path while still executing before Next's hydration
|
||||
module scripts (both ride the after-parse in-order queue, and
|
||||
this tag comes first), so `load-locale.ts` finds `self.__HL_I18N`
|
||||
set when the client bundle initializes — SSR text and first
|
||||
client render stay identical. The `?v=` build-version key makes
|
||||
the response immutable-cacheable: one catalog download per
|
||||
deploy, then HTTP/SW cache instead of per-document payload.
|
||||
*/}
|
||||
<script
|
||||
src={`/i18n/${initialLocale}?v=${process.env.NEXT_PUBLIC_APP_VERSION ?? "dev"}`}
|
||||
defer
|
||||
/>
|
||||
</head>
|
||||
<body className={`${inter.variable} font-sans antialiased`}>
|
||||
<Providers
|
||||
initialLocale={initialLocale}
|
||||
initialMessages={initialMessages}
|
||||
>
|
||||
<Providers initialLocale={initialLocale}>
|
||||
<MonitoringBootstrap />
|
||||
<WebVitalsReporter />
|
||||
{/* `DEMO_MODE` is a server-only env var; the proxy uses it to
|
||||
|
||||
Reference in new issue
Block a user