0
0
NextJSframework~10 mins

Font optimization and self-hosting in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Font optimization and self-hosting
Start Next.js Project
Add font files locally
Configure next.config.js for fonts
Import fonts in CSS or JS
Next.js builds with optimized fonts
Browser loads fonts from local server
Faster load, better control, no external requests
This flow shows how Next.js projects add local font files, configure them for optimization, and serve them from the local server for faster loading.
Execution Sample
NextJS
import localFont from 'next/font/local';

const myFont = localFont({
  src: './fonts/MyFont.woff2',
  display: 'swap',
});

export default function Home() {
  return <main className={myFont.className}>Hello!</main>;
}
This code imports a local font file, sets it up with font-display swap, and applies it to a component's main element.
Execution Table
StepActionResultEffect
1Import localFont from next/font/locallocalFont function readyPrepare to load local font
2Call localFont with src './fonts/MyFont.woff2' and display 'swap'myFont object created with classNameFont CSS class generated
3Use myFont.className in <main> elementmain element gets font CSS classText will use the local font
4Next.js build processes fontFont optimized and included in buildFont served from local server
5Browser loads pageFont CSS class appliedText rendered with local font quickly
6Font-display swap triggersFallback font shows until local font loadsSmooth font loading without invisible text
7Page fully loaded with local fontText uses MyFont.woff2Improved performance and control
💡 Font is fully loaded and applied, no external font requests needed
Variable Tracker
VariableStartAfter Step 2After Step 3Final
myFontundefined{className: 'font_xyz'}{className: 'font_xyz'}{className: 'font_xyz'}
Key Moments - 3 Insights
Why do we use 'display: swap' in the font config?
Using 'display: swap' ensures the browser shows fallback text immediately and swaps to the local font once loaded, avoiding invisible text. See execution_table step 6.
How does Next.js serve the font locally instead of from Google Fonts?
By importing the font file locally and configuring next/font/local, Next.js bundles the font in the build and serves it from your server, as shown in execution_table step 4.
What does myFont.className represent and how is it used?
myFont.className is a CSS class generated by Next.js that applies the font styles. It is added to the element's className to use the font, as in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of myFont after step 2?
AA string with font file path
BUndefined
CAn object with a className property
DA React component
💡 Hint
Check variable_tracker row for myFont after Step 2
At which step does the browser start using the local font for text rendering?
AStep 4
BStep 7
CStep 5
DStep 3
💡 Hint
Look at execution_table step 7 describing final font usage
If we remove 'display: swap' from the font config, what changes in the execution?
AText might be invisible until font loads
BFont will load faster
CNo change in font loading behavior
DFont will load from Google Fonts instead
💡 Hint
Refer to key_moments about 'display: swap' and execution_table step 6
Concept Snapshot
Next.js font optimization with self-hosting:
- Import fonts locally using next/font/local
- Configure with src and display options
- Use generated className on elements
- Next.js bundles and serves fonts locally
- 'display: swap' avoids invisible text
- Results in faster, controlled font loading
Full Transcript
This visual execution shows how Next.js handles font optimization and self-hosting. First, you import the localFont function from next/font/local. Then you call it with your local font file path and set display to 'swap' to improve loading behavior. This creates a font object with a CSS class name. You apply this class name to your component's element. During build, Next.js optimizes and bundles the font to serve it from your server. When the browser loads the page, it applies the font class, showing fallback text immediately and swapping to the local font once loaded. This avoids invisible text and improves performance by removing external font requests. Variables like myFont hold the font class name throughout. Key moments include understanding why 'display: swap' is important and how Next.js serves fonts locally instead of from external sources. The quiz tests your understanding of these steps and variable states.