Why do web font loading strategies that use font-display swap sometimes create CLS spikes that exceed the good threshold even with preloading?
Answered 3-minute answer rooted in 2 pages
Because preloading changes when the web font arrives, not what happens when it does. font-display: swap shows text in a fallback font immediately and replaces it with the web font whenever that font loads. web.dev explains the cost: font swapping can “cause layout shifts” when “a web font and its fallback font take up different amounts of space on the page”. If the two fonts have different widths or line heights, the swap re-wraps text and moves everything below it. A preload makes the swap happen sooner; on a page where the swap happens after the text is visible, it still shifts the layout.
Why preloading is not enough
swap has a block period of 0 milliseconds and an unlimited swap period, according to web.dev’s font best practices. The browser never waits for the web font before showing text, and it swaps in the web font whenever it arrives, however late.
A preload starts the download earlier. On a fast connection, the font may arrive before the text first paints, and no shift happens. On a slower connection or a slower device, the fallback text paints first and the font arrives afterward. The swap then happens on visible text. Field data at the 75th percentile, where Google’s CLS threshold of 0.1 applies, includes those slower visits, which is why a page can look stable in the lab and still fail in the field.
Why the shift can be large
web.dev’s CLS article scores each shift as impact fraction times distance fraction: how much of the viewport the moving content occupies and how far it moves. A font swap in a headline or the first paragraphs can move a large block of text, plus every element below it, by a line or more. On a phone, where the text fills most of the viewport, one swap can produce a score near or above the threshold.
The shift counts because it is unexpected. CLS forgives only shifts that follow a tap, click or key press by less than half a second, and a font swap during load has no such input.
Ways to stop the swap from shifting the page
web.dev’s font guidance names the main options:
- Use
font-display: optionalwhere the font is not essential. The guide calls it the most “performant” choice: text render is delayed by at most 100 milliseconds, and there is “assurance that there isn’t font-swap related layout shift”. If the font is not ready in time, the fallback stays for that page view. - Make the fallback the same size as the web font. The guide points to the
size-adjustdescriptor as a way to reduce the CLS impact by making the fallback font occupy the same space. When the two match, the swap changes the look of the text but not its layout. - Deliver the font earlier. Preloading and self-hosting can help the font arrive before first paint more often. They reduce how often the swap is visible, not the size of the shift when it is.
- Limit swapping to where it matters. The guide suggests reserving
swapfor visually distinctive elements and usingoptionalfor body text.
How to confirm the cause
- Check which elements shifted in field data with CLS attribution. A font swap shows up as text blocks moving shortly after first paint.
- Reproduce on a throttled connection with the cache cleared, so the font arrives after first paint.
- Compare fallback and web font metrics on the affected text, and test
size-adjustuntil the swap no longer moves the layout.
Preloading is worth keeping. It makes the font arrive sooner; matching the fallback’s size, or using optional, is what keeps the swap from moving the page.
Leave a Reply