π€ Next.js 16 Font Optimization Deep Dive
FOIT, FOUT, next/font, Self-Hosting & Rendering Internals
Overview
Fonts web design ka important part hain, lekin font loading sirf visual design ka topic nahi hai.
Font loading directly affect karta hai:
- Performance
- User Experience (UX)
- Core Web Vitals
- Perceived Speed
- Accessibility
Kai baar page technically fast load ho jata hai, lekin font loading ki wajah se user ko site slow feel hoti hai.
Isi problem ko solve karne ke liye Next.js next/font system provide karta hai.
Fonts Actually Kaise Work Karte Hain?
Browser ke paas har font available nahi hota.
Agar aap use karte ho:
- Inter
- Poppins
- Roboto
- Open Sans
- Geist
To browser ko font file download karni padti hai.
Example:
font-family: "Inter";Browser ka simplified workflow:
HTML
β
CSS Parse
β
Font Discovery
β
Font Download
β
Text RenderYahin se ek important question aata hai:
Font download hone tak text ka kya hoga?
Isi question ka answer hai FOIT aur FOUT.
The Font Rendering Problem
Suppose browser ne text discover kar liya.
Lekin required font abhi download nahi hui.
Browser ke paas do options hain:
Option 1
Wait karo.
Option 2
Temporary fallback font dikhao.
In dono approaches ke naam hain:
- FOIT
- FOUT
FOIT (Flash Of Invisible Text)
FOIT ka full form hai:
Flash Of Invisible TextBehaviour
Browser decide karta hai:
Jab tak font download nahi hota, text mat dikhao.
Result:
Page Loaded
β
Text Hidden
β
Font Download
β
Text VisibleUser Experience
User dekhta hai:
β Blank text area
β Empty headings
β Lag feeling
β Slow website impression
Example
Suppose page mein heading hai:
<h1>Next.js Mastery</h1>Font download hone tak:
Nothing VisibleUser ko lag sakta hai ki page abhi load hi nahi hua.
FOUT (Flash Of Unstyled Text)
FOUT ka full form hai:
Flash Of Unstyled TextBehaviour
Browser decide karta hai:
Wait mat karo. Pehle fallback font dikhao.
Flow:
Fallback Font
β
Font Download
β
Font SwapUser Experience
β Text immediately visible
β Faster perceived performance
β Slight visual change possible
Example
Initial Render:
ArialFont Loaded:
InterUser ko font change dikh sakta hai.
FOIT vs FOUT
| Feature | FOIT | FOUT |
|---|---|---|
| Text Visibility | Hidden | Visible |
| Perceived Speed | Slow | Fast |
| UX | Poor | Better |
| Layout Stability | Better | Slight shift possible |
| User Preference | Rarely preferred | Usually preferred |
Mental Model
Restaurant Analogy:
FOIT
Customer ko tab tak kuch nahi diya jata jab tak complete meal ready na ho.
Wait
β
FoodFOUT
Customer ko pehle starter de diya jata hai.
Main course baad mein aata hai.
Starter
β
Main CourseMost users FOUT ko FOIT se zyada prefer karte hain.
Traditional Font Loading
Historically fonts usually load hote the:
<link
rel="stylesheet"
href="https://fonts.googleapis.com/..."
>Internal Flow
HTML
β
CSS
β
Google Fonts CSS
β
Font Files
β
RenderProblems
Extra Network Requests
Browser ko additional CSS request karni padti hai.
Third-Party Dependency
Rendering Google Fonts infrastructure par depend karti hai.
Longer Critical Path
Font render hone se pehle zyada steps complete karne padte hain.
Why next/font Exists
Next.js ne font loading ko optimize karne ke liye dedicated system introduce kiya:
import { Inter } from "next/font/google";Goal:
Font loading ko automatic aur optimized banana.
Basic Usage
import { Inter } from "next/font/google";
const inter = Inter({
subsets: ["latin"],
});
export default function RootLayout({
children,
}) {
return (
<html lang="en">
<body className={inter.className}>
{children}
</body>
</html>
);
}Surface par lagta hai:
Font Class AppliedLekin internally kaafi optimization hoti hai.
What Happens Internally?
Build process ke dauran Next.js font configuration analyze karta hai.
Step 1: Font Discovery
Build Start
β
Inter Font FoundStep 2: Font Asset Collection
Required font files identify ki jati hain.
Regular
Medium
Boldsirf wahi files include hoti hain jo actually use ho rahi hain.
Step 3: Self Hosting
Font files application ke saath host ki jati hain.
Step 4: Automatic Preload
Critical fonts preload ki ja sakti hain.
Step 5: Optimized CSS Generation
Next.js optimized font CSS generate karta hai.
Self Hosting Explained
Traditional approach:
Browser
β
Google Fonts
β
Font Filenext/font approach:
Browser
β
Your Application
β
Font FileBenefits
Better Privacy
Google Fonts requests avoid ho sakti hain.
Fewer External Dependencies
Third-party request chain reduce hoti hai.
Better Performance
Network hops kam ho jate hain.
Better Reliability
External service failures ka impact kam hota hai.
Font Preloading
Preload browser ko early hint deta hai.
Example:
Ye resource important hai.
Isko jaldi download karo.Without Preload
HTML
β
CSS
β
Font Discovery
β
DownloadWith Preload
HTML
β
Preload Trigger
β
Download Starts EarlyResult
Faster text rendering.
display: swap
Font loading strategy ka important concept.
Swap Behaviour
Fallback Font
β
Real FontBenefits
β Immediate text visibility
β Better perceived performance
Drawback
β Minor visual change possible
Why It Is Preferred
Most users:
- Invisible text pasand nahi karte
- Slight font change tolerate kar lete hain
Isi wajah se modern web generally swap strategy prefer karta hai.
Font Loading and UX
Font loading directly influence karta hai:
Readability
Text kitni jaldi readable hoti hai.
Perceived Performance
Site kitni fast feel hoti hai.
User Trust
Blank content users ko site slow lagne lagti hai.
Important Observation
Actual performance aur perceived performance alag cheezein hain.
Example:
Actual Load Time
β
2 SecondsLekin agar text immediately visible ho:
Feels FastProduction Thinking
Font optimization sirf technical topic nahi hai.
Design decisions bhi performance ko affect karte hain.
Bad Example
10 Font FamiliesHar font:
- More requests
- More files
- More rendering work
Better Approach
Interor
GeistUsually enough.
Font Weights Matter
Ye bhi common mistake hai.
Example:
100
200
300
400
500
600
700
800
900Har weight extra font data add kar sakta hai.
Better Strategy
Sirf required weights use karo.
Example:
400
700Variable Fonts
Modern font systems variable fonts support karte hain.
Instead of:
Multiple Font FilesUse:
Single Variable FontBenefits
- Smaller payload
- Better flexibility
- Easier management
Geist aur Inter variable font support provide karte hain.
Best Practices
Use next/font
Preferred approach.
Limit Font Families
Kam families = better performance.
Limit Font Weights
Sirf required weights load karo.
Prefer Variable Fonts
Where available.
Let Next.js Handle Preloading
Manual optimization ki zarurat kam padti hai.
Always Define Fallback Fonts
Example:
font-family:
"Inter",
Arial,
sans-serif;Common Mistakes
Multiple Font Families
Unnecessary downloads.
Loading Every Weight
Payload increase.
Using Google Font Links Directly
Optimization benefits lose ho jate hain.
Ignoring Lighthouse Warnings
Font issues often directly performance scores ko impact karte hain.
Mixing Too Many Design Fonts
Visual inconsistency + performance cost.
Final Mental Model
Fonts sirf design asset nahi hain.
Fonts rendering assets hain.
FOIT
Invisible TextFOUT
Visible Fallback Textnext/font
Automatically manage karta hai:
Font Discovery
β
Self Hosting
β
Preloading
β
Optimized CSS
β
Faster RenderingGolden Rule
Users font ko notice nahi karte jab sab kuch sahi kaam karta hai.
Lekin users immediately notice karte hain jab text late render ho, invisible ho, ya page slow feel ho.
Isi liye font optimization design concern se zyada performance concern hai.