/** * Web-font preload + `@font-face` rendering helpers * (stacksjs/stacks#283 — prevent layout shifts). * * Layout shifts (the LS in CLS) happen when a page renders with the * fallback font, then re-flows once the web font loads. The two * mitigations this module ships against that: * * 1. **Preload** — emit `` * for each font listed in the UI config, so the browser starts the * font download in parallel with HTML parsing rather than waiting * to discover the URL via CSS. * * 2. **`font-display: swap`** — the default `@font-face` block uses * `font-display: swap`, which shows the fallback font immediately * and swaps in the web font once available. Layout still shifts * slightly at swap time, but the page is readable from t=0 instead * of being blocked by the font fetch. * * Apps drop a list of fonts into `config/ui.ts` and call these helpers * from their layout's `
` — see {@link renderFontHead}. Both * outputs are safe to inline into HTML (no user-controlled values * interpolate; the FontEntry shape is config-only). */ /** One configured web font. */ export interface FontEntry { /** * Font family name. Used as the `font-family` value in the emitted * `@font-face` block and referenced from your CSS. */ family: string /** * Absolute or app-relative URL to the font file. Local files under * `public/` are preferred for the preload path since they share the * connection with the document; CDN-hosted fonts work but lose the * preload benefit when crossing origins without `crossorigin`. */ src: string /** * Font file format passed to the `format(...)` clause. Defaults to * `'woff2'` because every shipping browser supports it; only set * this when the asset is actually a different format. * * @default 'woff2' */ format?: 'woff2' | 'woff' | 'truetype' | 'opentype' /** * `font-weight` to apply on the `@font-face` block. Accepts CSS * keywords (`'normal'` / `'bold'`) or numeric weights (100-900). * * @default 'normal' */ weight?: number | string /** * `font-style` on the `@font-face` block. * * @default 'normal' */ style?: 'normal' | 'italic' | 'oblique' /** * `font-display` strategy. `'swap'` is the right default for the CLS * goal — see https://web.dev/font-display. `'optional'` is stricter * (skip the swap entirely on slow networks) and useful for highly * brand-sensitive surfaces; `'block'` re-introduces FOIT and should * be avoided unless you have a very specific reason. * * @default 'swap' */ display?: 'auto' | 'swap' | 'block' | 'fallback' | 'optional' /** * Whether to emit a `` tag. Defaults * to `true` for the first 2 fonts (the typical "above-the-fold" * count before TCP slot exhaustion hurts more than it helps) — set * `false` on long lists' tail entries. */ preload?: boolean /** * Optional `unicode-range`. Useful for subsetted fonts (e.g. Latin * vs CJK). Omitted from the `@font-face` block when undefined. * * @example * 'U+0000-00FF, U+0131, U+0152-0153' */ unicodeRange?: string } /** Map `format` shorthand to the MIME-style label CSS expects. */ const FORMAT_LABEL: Record