# <Font>

> Zero-roundtrip font hosting and layout shift prevention.

Subject: Next.js | Lesson 50 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/font-optimization
Previous: [<Image> (Advanced)](https://codewithanimation.in/nextjs/image-advanced.md) | Next: [<Script>](https://codewithanimation.in/nextjs/script-optimization.md)

## The next/font module

`next/font` includes built-in loaders for any Google Font or local custom font.

Instead of the browser making a network request to `fonts.googleapis.com` when the user visits your site, Next.js downloads the font files exactly once at build time.

The fonts are then hosted alongside your static assets, requiring zero external network roundtrips.

## Layout Shift & Flash of Unstyled Text

When using standard web fonts, the browser often shows a system fallback font (like Arial) while the custom font downloads.

Because Arial and your custom font have different character widths, swapping them causes the entire paragraph to reflow and shift down the page, ruining the user experience (CLS).

Next.js completely eliminates this by automatically generating a CSS `size-adjust` property that perfectly scales the fallback font to match the custom font's exact dimensions.

## Implementation

1. Import the font: `import { Inter } from 'next/font/google'`

2. Initialize it: `const inter = Inter({ subsets: ['latin'] })`

3. Apply it globally (usually in your Root Layout): `<body className={inter.className}>`

## Common mistake: Privacy & GDPR

In addition to performance issues, using standard `<link href="https://fonts.googleapis.com...">` tags sends your users' IP addresses to Google, which can be a GDPR violation.

`next/font` hosts the files on your own domain, completely removing the privacy risk.

## Try it: The CLS Simulator

Watch how a standard web font causes a jarring layout shift when the font swaps, compared to the `next/font` module which perfectly scales the fallback font using CSS size-adjust.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/font-optimization

## Check yourself

### Question 1

How does `next/font` prevent Layout Shift when the custom font finally loads?

- A. By hiding the text until the font is completely downloaded.
- B. By automatically injecting a CSS size-adjust property that perfectly scales the fallback font.
- C. By converting the font to an SVG.
- D. By forcing the browser to use a monospaced font.

Correct answer: B

Next.js analyzes the font metrics at build time and calculates a custom CSS `size-adjust` and `ascent-override` so the fallback font takes up the exact same physical space as the custom font.

- Why not A: This is called FOIT (Flash of Invisible Text) and is terrible for performance.
- Why not C: Fonts remain as woff2 files.
- Why not D: It scales any fallback font dynamically.

## Remember this

- `next/font` downloads Google Fonts at build time and hosts them locally.
- It prevents Cumulative Layout Shift (CLS) by automatically scaling the fallback font.
- It improves privacy by removing external network requests to Google.
- Apply the font's generated `className` in your Root Layout to apply it globally.
