Font Loading Delay — Swap/Block/FOIT Fix
In this tutorial, you'll learn about Font Loading Delay. We cover key concepts, practical examples, and best practices.
The Problem
Web fonts can delay text rendering in two ways: Flash of Invisible Text (FOIT) where the browser hides text for up to 3 seconds while the font loads, or Flash of Unstyled Text (FOUT) where text renders in a fallback font before swapping. Both hurt performance and user experience.
Quick Fix
Step 1: Use font-display: swap
/* Wrong — default is block (FOIT up to 3 seconds) */
@font-face {
font-family: 'Open Sans';
src: url('/fonts/open-sans.woff2') format('woff2');
}
/* Right — show fallback immediately, swap when font loads */
@font-face {
font-family: 'Open Sans';
src: url('/fonts/open-sans.woff2') format('woff2');
font-display: swap;
}
/* Use with matching fallback */
body {
font-family: 'Open Sans', system-ui, sans-serif;
}
Step 2: Preload critical fonts
<!-- Wrong — font discovered late, delays rendering -->
<link rel="stylesheet" href="fonts.css">
<!-- Right — preload the font file -->
<link
rel="preload"
href="/fonts/open-sans.woff2"
as="font"
type="font/woff2"
crossorigin
>
<!-- For multiple font weights -->
<link rel="preload" href="/fonts/open-sans-400.woff2" as="font" crossorigin>
<link rel="preload" href="/fonts/open-sans-700.woff2" as="font" crossorigin>
Step 3: Subset fonts to reduce file size
/* Wrong — full font file with all characters */
@font-face {
font-family: 'Open Sans';
src: url('/fonts/open-sans.woff2') format('woff2');
}
/* Right — use subsetted font for Latin-only pages */
@font-face {
font-family: 'Open Sans';
src: url('/fonts/open-sans-latin.woff2') format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
Step 4: Use optional for non-critical fonts
/* Wrong — all fonts use swap, causing layout shifts */
@font-face {
font-family: 'Decorative';
src: url('/fonts/decorative.woff2');
font-display: swap;
}
/* Right — use optional for decorative fonts */
@font-face {
font-family: 'Decorative';
src: url('/fonts/decorative.woff2');
font-display: optional;
/* If font doesn't load within 100ms, use fallback permanently */
}
Step 5: Match fallback metrics with size-adjust
/* Wrong — fallback metrics unknown, causes layout shift on swap */
@font-face {
font-family: 'Custom';
src: url('/fonts/custom.woff2');
font-display: swap;
}
/* Right — adjust fallback metrics to match */
@font-face {
font-family: 'Custom';
src: url('/fonts/custom.woff2');
font-display: swap;
size-adjust: 98%;
ascent-override: 92%;
descent-override: 24%;
}
Prevention
- Use
font-display: swapfor body text fonts - Preload only the most critical font files (1-2 weights)
- Subset fonts to reduce file size (include only needed characters)
- Use
font-display: optionalfor decorative or non-critical fonts - Match fallback font metrics with
size-adjustto prevent layout shift
Common Mistakes with font delay
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world PERF code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro