Files
HealthLog/src/__tests__/self-hosted-font-guard.test.ts
T
Marc-André Bombeck 5d3794e734 build(fonts): self-host Inter so the build fetches nothing
next/font/google downloads Inter from fonts.googleapis.com and
fonts.gstatic.com while the app builds, and when that request fails the
build fails with it, which is what broke the arm64 image build and
several CI jobs. The files it downloaded now live in src/app/fonts/inter
under the SIL Open Font License, and next/font/local declares them.

They are the same bytes: Inter 4.001, one variable file per script
subset (Cyrillic and its extension, Greek and its extension,
Vietnamese, Latin Extended, Latin), each used for weights 400 to 700.
The declarations rebuild the stylesheet Google answered, the twenty-eight
faces with Google's unicode ranges in Google's subset order, swap
display, and only the Latin file preloaded. The local loader emits one
unicode range per call, so each subset is its own call under the
family Inter.

The Arial fallback face and the font-sans stack move into globals.css
with the metric overrides next/font/google shipped for Inter; the local
loader would have computed slightly different ones from the file.

A production build with every outbound request sent to a dead proxy
completes, where the same build with next/font/google fails on the
fonts.googleapis.com request. The content security policy already
allowed fonts from self only. A guard refuses a next/font/google import
anywhere under src.
2026-10-22 07:31:16 +02:00

59 lines
2.2 KiB
TypeScript

/**
* The build fetches no font from the network.
*
* `next/font/google` downloads its files from fonts.gstatic.com while the app
* builds, and that download failing is what broke image builds. Inter is
* committed under `src/app/fonts/inter/` instead, under its licence, and
* declared through `next/font/local`. This guard keeps it that way: no source
* file imports the Google loader, every file the local declarations name is
* in the tree, and each subset carries the four weights the app was built
* with.
*
* Mutation check: importing `Inter` from the Google loader anywhere under
* `src/` fails the first case; deleting a `.woff2` or dropping a weight from a
* subset fails the second.
*/
import { existsSync } from "node:fs";
import { dirname, join } from "node:path";
import { describe, expect, it } from "vitest";
import { readSource, readSourceFolder } from "./helpers/source-files";
const ROOT = process.cwd();
const FONT_MODULE = "src/app/fonts/inter.ts";
describe("self-hosted fonts", () => {
it("imports no font loader that downloads at build time", () => {
const importing = readSourceFolder("src", {
floor: 3000,
include: (rel) => !rel.startsWith("generated/"),
extensions: [".ts", ".tsx", ".mts", ".js", ".mjs"],
})
.filter(([, text]) => /from\s+["']next\/font\/google["']/.test(text))
.map(([rel]) => rel);
expect(importing).toEqual([]);
});
it("ships every file the declarations name, with its licence and four weights", () => {
const source = readSource(FONT_MODULE, ["next/font/local", "localFont("]);
const entries = [
...source.matchAll(/path:\s*"([^"]+)",\s*weight:\s*"(\d+)"/g),
].map(([, path, weight]) => ({ path: path!, weight: weight! }));
expect(entries.length).toBe(28);
const byFile = new Map<string, string[]>();
for (const { path, weight } of entries) {
byFile.set(path, [...(byFile.get(path) ?? []), weight]);
}
expect(byFile.size).toBe(7);
for (const [path, weights] of byFile) {
expect(existsSync(join(ROOT, dirname(FONT_MODULE), path)), path).toBe(
true,
);
expect(weights, path).toEqual(["400", "500", "600", "700"]);
}
expect(existsSync(join(ROOT, "src/app/fonts/inter/OFL.txt"))).toBe(true);
});
});