Web Font Performance

Presentation

I'm Anthony BARRE

  • I'm a full stack developer at Fasterize
  • twitter : @AnthoBarre
  • github : abarre
  • Fasterize is an all-in-one website accelerator.
  • It is an automated web performance optimization service (SAAS) used by many high traffic websites.
    Fasterized websites are already HTTP/2 enabled and take fully part of the next gen web.
Presentation

Plan

What is the state of the art for font optimization ?

The presentation is based on works of studies done at Fasterize

  • Analysis based on work of Zach Leatherman
  • Analysis based on work of Bram Stein
General concept

Stats from HTTP archive April 2017

  • 99kb of font transfer size
  • around 3 font requests
  • 67% of sites using web fonts
General concept

Stats from Fasterize Feb 2017

We looked at the font loading performed by our main customers in the french ecommerce

General concept

Stats from Fasterize Feb 2017

Test with and without loading the fonts

General concept

Trigger a font download

A web font is only downloaded when it is used in a CSS selector that matches a DOM connected node with text.

Caution: italic and bold variations trigger different font download.

The browser chooses the format according to the ordered list of sources

General concept

Behavior while downloading a font

General concept

Behavior while downloading a font

General concept

Behavior while downloading a font

  • Loading font on Safari is kind of a SPOF
  • Nice default in term of performance for Microsoft browsers
  • Default timeout of 3 sec is very long
  • Bandwidth is consumed during the critical path

FOUT is progressive enhancement

General concept

Use only system fonts

Major websites moved from custom fonts to system fonts.

  • medium
  • Wordpress 4.6
  • ghost
  • github
General concept

Reduce the size of Font files

New bulletproof @font-face: WOFF + WOFF2. Done.

Subsettings : keep latin subset and unicode-range descriptor

Optimization

1 - font-display

Optimization

1 - font-display

  • font-display: block hides the text until the font is loaded (FOIT)
Optimization

1 - font-display

  • font-display: swap shows fallback font until the font is loaded (FOUT)
Optimization

1 - font-display

  • font-display: fallback will replace the fallback font only within a timeout
Optimization

1 - font-display

  • font-display: fallback will replace the fallback font only within a timeout
Optimization

1 - font-display

  • font-display: optional will load the font only if cached
Optimization

1 - font-display

  • font-display: optional will load the font only if cached
Optimization

1 - font-display

Cons

  • Browser support still limited.
  • Limited flexibility: no way to group requests or repaints

Pros

  • Chrome 60 will support it without flags
  • Very simple: only a single CSS descriptor added to your @font-face block
  • Good rendering performance (this would give us FOUT without any JS required)
  • Super future friendly: is orthogonal to web font formats
  • Does not require fonts modification

VERDICT: NO HARM IN ADDING NOW, BUT NOT ENOUGH FOR THE MOMENT.

Optimization

2 - preload fonts

Optimization

2 - preload fonts

Cons

  • Browser support still limited. (Blink only)
  • Network congestion: the more you preload, the more you can block initial render
  • Flexibility: no way to group repaints/reflows

Pros

  • Super easy to implement
  • Future friendly if you use the type attribute to specify the font format
  • Better rendering performance than a @font-face block
  • Super future friendly: is orthogonal to web font formats
  • Does not require fonts modification

VERDICT: NOT SUFFICIENT

Optimization

3 - FOUT with class

1. Trigger a font download with the CSS Font Loading API

2. Observe font download in CSS via the Font Face Observer lib

Optimization

3 - FOUT with class

Cons

  • Requires strict maintenance/control of your CSS
  • Typically requires you to hard code which web fonts you want to load on the page

Pros

  • Rendering performance: eliminates FOIT.
  • Flexibility: easy to group requests into a single repaint
  • Scalability: requests happen in parallel
  • Great browser support
  • Does not require modification of the fonts

VERDICT: THIS IS THE BASELINE STANDARD. THIS WILL WORK FOR MOST USE CASES

Optimization

4 - inline/preload critical FONT subset and lazyload the full font in an external CSS

The ultimate goal is to avoid FOIT and mostly eliminate FOUT.

  • Create a critical FONT with a romain weight, normal style and containing A-Z and optionally 0-9 and/or punctuation)
  • Lazyload other weights and styles and take advantages of faux-bold and faux-italic content (using font synthesis)
Optimization

4 - inline/preload critical FONT subset and lazyload the full font in an external CSS



Optimization

4 - inline/preload critical FONT subset and lazyload the full font in an external CSS


Optimization

4 - inline/preload critical FONT subset and lazyload the full font in an external CSS

Cons

  • Needs to manage the cache to avoid extra critical font loading for repeat view
  • Introduces a flash of false-text
  • Hard to automate
  • License restriction: requires subsetting

Pros

  • All advantages of the FOUT with class
  • Greately reduces the FOUT for the Roman font
  • Greately reduces the bandwidth consumed during the critical rendering path

VERDICT: THIS IS THE CURRENT GOLD STANDARD.

Anti Pattern

Inlining full fonts

Anti Pattern

Inlining full fonts

Cons

  • Breaks parallel downloads
  • Saturates bandwidth during the critical path
  • No progressive rendering
  • No caching

Pros

  • Seemingly great rendering performance: this approach has no FOUT or FOIT.
  • Robustness: inlining puts all your eggs into your initial server request
  • No HTTP overhead
Conclusion

  1. Fonts are enhancement
  2. The default browser behavior for font-loading is harmful to the perceived performance
  3. FOUT is a progressive enhancement
  4. Be prepared for the future : font-display and link rel="preload" of the regular fonts
  5. FOUT with class offers a good threshold

Web Font Optimization

Questions ?