Automatic font metric fallback for Nuxt. Powered by fontaine + capsize.
Custom web fonts cause Cumulative Layout Shift (CLS) while loading. This module reads your font files at build time, computes metric overrides (size-adjust, ascent-override, descent-override), and generates @font-face fallback declarations so text never jumps.
npm install nuxt-font-fallback// nuxt.config.ts
export default defineNuxtConfig({
modules: [
['nuxt-font-fallback', {
fonts: [
{ name: 'GothamOverride', src: 'public/fonts/Gotham-Book.otf' },
],
}],
],
})Then reference the override family in your CSS:
:root {
font-family: 'Gotham', GothamOverride;
}| Option | Type | Default | Description |
|---|---|---|---|
fonts |
FontEntry[] |
[] |
Custom fonts to generate fallbacks for |
fallbackFonts |
string[] |
['Arial', 'Helvetica Neue', 'Helvetica', 'Roboto', 'sans-serif'] |
System font stack; first is used for metrics |
cssFilename |
string |
'nuxt-font-fallback.css' |
Output CSS filename |
interface FontEntry {
/** @font-face font-family name for the fallback, e.g. 'GothamOverride' */
name: string
/** Path relative to srcDir, e.g. 'public/fonts/Gotham-Book.otf' */
src: string
}- Reads each font file via
fontaine'sreadMetrics - Retrieves the first fallback font's metrics from the capsize database
- Computes
size-adjust,ascent-override,descent-override - Generates
@font-facedeclarations with multi-platformsrc: local(...)chains - Injects the CSS globally before other styles
# Install dependencies
pnpm install
# Generate type stubs
pnpm dev:prepare
# Develop with the playground
pnpm dev
# Build the playground
pnpm dev:build
# Run ESLint
pnpm lint
# Run Vitest
pnpm test
pnpm test:watch
# Release new version
pnpm releaseMIT