Files
HealthLog/scripts/generate-i18n-boot.mjs
T
Marc-André Bombeck 9f2a3e25fe perf(i18n): generate the catalog boot scripts as static assets
The dynamic /i18n/[locale] route handler answered with Next's default
Cache-Control (private, max-age=0, must-revalidate) and no validator —
its immutable-cache header was overridden, so a non-PWA browser would
re-download the full catalog on every hard load. Static public/ files
keep an ETag (repeat loads revalidate to a 304) and the service
worker's cache-first rule covers the offline relaunch; the ?v= build
key the layout appends busts both per deploy.

scripts/generate-i18n-boot.mjs writes public/i18n/<locale>.js for all
six locales as a prebuild/predev step (the sw-version.js pattern);
output is gitignored. The route handler is removed.

Measured on a production build, authenticated DE session: dashboard
document 522 KB raw / 139 KB gz -> 170 KB raw / 24.6 KB gz; the DE
catalog moves into a one-time 119 KB gz cached asset.

H2
2026-07-07 13:15:37 +02:00

53 lines
2.0 KiB
JavaScript

#!/usr/bin/env node
/**
* Generate the locale-catalog boot scripts (`public/i18n/<locale>.js`).
*
* The root layout emits `<script src="/i18n/<locale>.js?v=<version>" defer>`
* ahead of Next's hydration scripts; the body assigns the active locale's
* full message bundle to `self.__HL_I18N`, which `src/lib/i18n/load-locale.ts`
* reads in its browser cache initializer. 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).
*
* Static `public/` files (rather than a route handler) because Next serves
* them with an ETag — repeat loads revalidate to a 304 instead of
* re-downloading the catalog — and the service worker caches the versioned
* URL cache-first for the offline relaunch. The `?v=` query the layout
* appends busts both caches per deploy.
*
* Runs as a `prebuild`/`predev` step (the `sw-version.js` pattern); output
* is gitignored. Content is the same public catalog JSON that ships in the
* repository — no tenant data, no secrets.
*/
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const repoRoot = join(__dirname, "..");
const outDir = join(repoRoot, "public", "i18n");
const LOCALES = ["de", "en", "fr", "es", "it", "pl"];
mkdirSync(outDir, { recursive: true });
const banner =
"// AUTO-GENERATED by scripts/generate-i18n-boot.mjs — do not edit.\n";
for (const locale of LOCALES) {
const messages = JSON.parse(
readFileSync(join(repoRoot, "messages", `${locale}.json`), "utf8"),
);
const payload = JSON.stringify({ locale, messages });
writeFileSync(
join(outDir, `${locale}.js`),
`${banner}self.__HL_I18N = ${payload};\n`,
"utf8",
);
}
process.stdout.write(
`generate-i18n-boot: wrote ${LOCALES.length} catalogs to public/i18n/\n`,
);