Performance
Fonts
Learn Next.js font optimization with next/font, local and Google fonts, CSS variables, and layout-shift-safe typography.
1. Learning Objectives
By the end of this lesson, you will be able to load optimized fonts with next/font, use local fonts, define CSS variables, and reduce font-related layout shift.
Difficulty: Beginner.
2. Prerequisites
3. Overview
Next.js font optimization helps load fonts efficiently and reduce layout shift. The next/font package can self-host supported Google fonts or load local font files with generated CSS.
4. Why This Topic Matters
Fonts can block rendering, shift text, and add extra network requests. Optimized font loading improves perceived speed and visual stability.
5. Real-World Analogy
Font optimization is like stocking the exact typeface in your print shop before opening. Customers do not wait while you run across town to find the letters.
6. Core Concepts
| Concept | Meaning |
|---|---|
next/font | Next.js font loading package. |
| Self-hosting | Serving font files from your app build output. |
| CSS variable | Custom property connected to a font family. |
| Fallback | Backup font used before custom font is ready. |
| Layout shift | Text movement caused by font metric differences. |
7. Syntax & API Reference
8. Visual Diagram
9. Live Example - Full Working Code
What just happened? A local font is loaded through the framework and exposed through a CSS variable.
10. Interactive Playground
Try this:
next/font/google.next/font/local.11. Common Mistakes
| Mistake | Why It Happens | Correct Approach |
|---|---|---|
Loading fonts with random tags | Familiar from plain HTML. | Use next/font when possible. |
| Loading too many weights | Design exploration goes unchecked. | Load only weights/styles used. |
| Applying font variable in wrong place | Layout structure is misunderstood. | Put variables high enough, often on . |
12. Best Practices
next/font for app fonts.woff2 for local fonts.13. Browser Compatibility
| Feature | Browser Impact | Notes |
|---|---|---|
woff2 | Broad modern support | Preferred font format. |
| CSS variables | Broad modern support | Useful for theme tokens. |
| Font fallback | Standard CSS | Always define sane fallbacks. |
14. Interview Questions
Easy: What package optimizes fonts in Next.js?Answer: next/font.
Answer: Each weight can add bytes and loading cost.
Hard: Why do font metrics matter?Answer: Different fallback and final font metrics can move text and cause layout shift.
15. Debugging Exercise
Bug report: "The site flashes text and shifts when the font loads."
Solution
Use next/font, reduce weights, ensure fallback metrics are handled, and apply the font class/variable consistently at layout level.
16. Practice Exercises
17. Scenario-Based Challenge
A brand page loads five font families and nine weights. What should change?
Walkthrough
Reduce families and weights to the actual design system, use next/font, and verify layout stability and network cost.
18. Quick Quiz
1. Font package? Answer: next/font.
2. Preferred local format? Answer: woff2.
3. Should every weight be loaded? Answer: No.
4. Where often place font variables? Answer: Root layout.
5. What metric can font swaps hurt? Answer: CLS.
19. Summary & Key Takeaways
next/font provides optimized loading.20. Cheat Sheet
| Need | Tool |
|---|---|
| Google font | next/font/google |
| Local font | next/font/local |
| Design token | variable |
| Reduce bytes | Fewer weights/subsets |
| Stable text | Good fallback metrics |
21. Further Reading
22. Next Lesson Preview
Next, you will learn lazy loading for components and libraries.