Under The Hood
frontend nextjs Next.js 14+ / 15

next/font CSS Variable Self-Hosting & Metric Overrides

Last updated
Prerequisites:
CSS `@font-face` rules
Cumulative Layout Shift (CLS) basics
  • nextjs
  • next-font
  • cls
  • web-vitals
  • fonts
  • performance

Read at your depth

The practical view

`next/font` automatically self-hosts Google Fonts or local font files at build time. It downloads the font files during build and serves them from your domain, eliminating external Google network requests. It injects CSS with fallback font metric overrides (`size-adjust`, `ascent-override`) matching the fallback system font to the web font, eliminating Cumulative Layout Shift (CLS).

The same idea in other frameworks

react equivalent
Standard `@font-face` with manual font-pie / Capsize CSS metric overrides.

Legacy vs modern

External Google Font CDN vs next/font Self-Hosting

External `<link>` tags introduce layout shift and extra DNS lookups; `next/font` self-hosts and generates metric overrides.

before → after
External CDN Link
<link href="https://fonts.googleapis.com/..." rel="stylesheet"> // ❌ OLD: Third-party DNS + CLS
next/font with Variable
import { Inter } from 'next/font/google'; // ✅ NEW: Self-hosted + 0 CLS

Interview gotchas

Context

This tests knowledge of CSS font metric overrides and Capsize/font-pie mathematics.

The mechanical answer

`font-display: swap` normally causes layout shift when the custom font swaps in because fallback fonts (Arial, Times) have different glyph widths and line heights. `next/font` analyzes the custom font's metrics and injects an adjusted fallback `@font-face` using CSS properties like `size-adjust`, `ascent-override`, and `descent-override`, perfectly matching the fallback's box dimensions to the custom font.

Trap

Do not say that `next/font` avoids CLS by blocking rendering until the font downloads. It uses `font-display: swap` with metric-matched fallbacks.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.