Skip to content

Font Loading Delay — Swap/Block/FOIT Fix

DodaTech Updated 2026-06-24 3 min read

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: swap for 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: optional for decorative or non-critical fonts
  • Match fallback font metrics with size-adjust to prevent layout shift

Common Mistakes with font delay

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. 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

### What is the difference between FOIT and FOUT?

FOIT (Flash of Invisible Text): browser hides text for up to 3 seconds while the font loads (default font-display: block). FOUT (Flash of Unstyled Text): browser shows fallback font immediately, swaps when custom font loads (font-display: swap). FOUT is better for perceived performance.

How does font-display: optional work?

The browser has ~100ms to load the font. If it loads, great. If not, the fallback font is used permanently for that page visit. On subsequent visits, the font is likely cached and loads immediately. This is ideal for non-critical or decorative fonts.

What font formats should I serve?

Use WOFF2 with WOFF fallback. WOFF2 offers ~30% better compression than WOFF. Convert TTF/OTF fonts to WOFF2 using tools like Google Webfonts Helper or FontSquirrel. Avoid TTF and EOT formats in modern web development.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro