Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Performance and Usability of Font Loading (Velocity NYC 2015)

zachleat
October 13, 2015

The Performance and Usability of Font Loading (Velocity NYC 2015)

A deep dive into @font-face for Velocity 2015 in New York City.

http://velocityconf.com/devops-web-performance-ny-2015/public/schedule/detail/46234

zachleat

October 13, 2015
Tweet

More Decks by zachleat

Other Decks in Programming

Transcript

  1. THE DEFAULT BROWSER BEHAVIOR FOR FONT-LOADING IS HARMFUL TO BOTH

    THE PERCEIVED PERFORMANCE AND INTEGRITY OF CONTENT ON THE WEB. FONT-LOADING INTEGRITY PERCEIVED PERFORMANCE
  2. IMG

  3. CSS

  4. A G EN D A TRIGGER A FONT DOWNLOAD BEHAVIOR

    WHILE DOWNLOADING 1.   2.  
  5. @font-­‐face  {    font-­‐family:  The  Font  that  Could;    src:

     url('icanfont.woff2')            format('woff2'),          url('icanfont.woff')            format('woff'),          url('icanfont.otf')            format('opentype');   }
  6. IS 30% SMALLER THAN WOFF On average across all of

    Google Fonts: https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/j27Ou4RtvQI/discussion WOFF2
  7. <!doctype  html>   <style>   @font-­‐face  {    font-­‐family:  My

     Web  Font;    src:  url('icanfont.woff2')  format('woff2'),        url('icanfont.woff')  format('woff');   }   </style>  
  8. 8 36+ 23+ 6.1+ 2.3+ 7+ DOWNLOADS WITH EMPTY ATTACHED

    ELEMENT DOWNLOADS EVERY TIME DOWNLOADS WITH NON-EMPTY ATTACHED ELEMENT 28+ 9+ &
  9. BASIC LATIN CODE POINTS Arial Chunk Five x26 & &

    x41 A A x42 B B x43 C C x2B + + x3F ? ?
  10. @font-­‐face  {     font-­‐family:  Ampersand;     src:  url('sands.woff2')

     format('woff2'),                url('sands.woff')  format('woff');      unicode-­‐range:  U+26;   } unicode- range
  11. <!doctype  html>   <style>   @font-­‐face  {    font-­‐family:  My

     Web  Font;    src:  url('icanfont.woff2')  format('woff2'),        url('icanfont.woff')  format('woff');      font-­‐weight:  400;      font-­‐style:  normal;   }   </style>  
  12. <style>   p  {      font-­‐family:  My  Web  Font;

         font-­‐weight:  400;      font-­‐style:  normal;   }   </style>   <p>Roman  Text</p>
  13. @font-­‐face  {      font-­‐family:  My  Web  Font;    

     font-­‐weight:  400;      font-­‐style:  normal;   } p  {      font-­‐family:  My  Web  Font;      font-­‐weight:  400;      font-­‐style:  normal;   }
  14. @font-­‐face  {      font-­‐family:  My  Web  Font;    

     font-­‐weight:  400;      font-­‐style:  normal;   } p  {      font-­‐family:  My  Web  Font;      font-­‐weight:  400;      font-­‐style:  normal;   } ✔
  15. 100  200  300  400  500  600  700  800  900 FONT-WEIGHT

    DEFAULT  CSS   font-­‐weight:  normal; DEFAULT  CSS   font-­‐weight:  bold; ‐ ‑ ‑ ‐ Lighter  weights Bolder  weights
  16. @font-­‐face  {  font-­‐weight:  500;  font-­‐style:  normal;  }   @font-­‐face  {

     font-­‐weight:  400;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  300;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  200;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  100;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  600;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  700;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  800;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  900;  font-­‐style:  normal;  }   /*  REPEAT  THE  SAME,  WITH  font-­‐style:  italic;  */ p  {      font-­‐weight:  500;      font-­‐style:  normal;   }
  17. 100  200  300  400  500  600  700  800  900 FONT-WEIGHT

    <p> ‐ ‑ ‐ Lighter  weights Bolder  weights
  18. MisMatch weight 600 Available, 100 assigned, FORCED BOLD p  {

         font-­‐family:  My  Web  Font;      font-­‐weight:  100;      font-­‐style:  normal;   } @font-­‐face  {      font-­‐family:  My  Web  Font;      font-­‐weight:  600;      font-­‐style:  normal;   }
  19. MISMATCH STYLE ITALIC AVAILABLE, NORMAL ASSIGNED, FORCED ITALIC p  {

         font-­‐family:  My  Web  Font;      font-­‐weight:  400;      font-­‐style:  normal;   } @font-­‐face  {      font-­‐family:  My  Web  Font;      font-­‐weight:  400;      font-­‐style:  italic;   }
  20. @font-­‐face  {  font-­‐weight:  600;  font-­‐style:  normal;  }   @font-­‐face  {

     font-­‐weight:  700;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  800;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  900;  font-­‐style:  normal;  }   /*  FAUX  BOLD  */   @font-­‐face  {  font-­‐weight:  500;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  400;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  300;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  200;  font-­‐style:  normal;  }   @font-­‐face  {  font-­‐weight:  100;  font-­‐style:  normal;  }   /*  END  FAUX  BOLD  */   /*  REPEAT  THE  SAME,  WITH  font-­‐style:  italic;  */ p  {      font-­‐weight:  600;      font-­‐style:  normal;   }
  21. 100  200  300  400  500  600  700  800  900 FONT-WEIGHT

    <p> ‐ ‑ ‐ Normal/Roman  @font-­‐face Bold  @font-­‐face Faux  Bold
  22. MisMatch weight 500 Available, 600 assigned, FAUX BOLD p  {

         font-­‐family:  My  Web  Font;      font-­‐weight:  600;      font-­‐style:  normal;   } @font-­‐face  {      font-­‐family:  My  Web  Font;      font-­‐weight:  500;      font-­‐style:  normal;   }
  23. MISMATCH STYLE p  {      font-­‐family:  My  Web  Font;

         font-­‐weight:  400;      font-­‐style:  italic;   } @font-­‐face  {      font-­‐family:  My  Web  Font;      font-­‐weight:  400;      font-­‐style:  normal;   } NORMAL AVAILABLE, ITALIC ASSIGNED, FAUX ITALIC
  24. <style>   @font-­‐face  {      font-­‐weight:  400;    

     font-­‐style:  normal;   }   </style>   <p>      Roman  Text      <strong>Bold  Text</strong>      <b>Bold  Text</b>      <em>Italic  Text</em>      <i>Italic  Text</i>   </p>
  25. <i>

  26. i,  cite,  em,  var,   address,  dfn,   strong,  b,

     h1,  h2,   h3,  h4,  h5,  h6,  th
  27. Matching Font-Family USED IN DOCUMENT CONTENT FONT-WEIGHT FONT-STYLE ✔ Images

    courtesy of http://www.w3.org/TR/css3-fonts/ ✔ ✔ ✔
  28. A G EN D A TRIGGER A FONT DOWNLOAD BEHAVIOR

    WHILE DOWNLOADING 1.   2.  
  29. People will visit a Web site less often if it

    is slower than a close competitor by more than 250 milliseconds.” http://www.nytimes.com/2012/03/01/technology/impatient-web-users-flee-slow-loading-sites.html “
  30. FOUT DO NOTHING EMBRACE BROWSER DEFAULTS 0 SECOND TIMEOUT FOUT

    FOIT N SECOND TIMEOUT FOIT InFINITE TIMEOUT N  >  0
  31. <style>   h1  {  font-­‐family:  serif;  }   .loaded  h1

     {  font-­‐family:  My  Web  Font;  }   </style>   <link  href="fonts.css"   onload="document.body.className+='   loaded';"  rel="stylesheet">  
  32. @font-­‐face  {      font-­‐family:  My  Web  Font;    

     src:  url('icanfont.woff')  format('woff');   }   @font-­‐face  {      font-­‐family:  My  Web  Font;      src:  url('icanfontbold.woff')  format('woff');      font-­‐weight:  700;   }
  33. <style>   h1  {  font-­‐family:  serif;  }   .loaded  h1

     {  font-­‐family:  My  Web  Font;  }   </style>   <link  href="fonts.css"  media="none"   onload="document.body.className+='   loaded';  this.media='all'"   rel="stylesheet">  
  34. @font-­‐face  {      font-­‐family:  My  Web  Font;    

     src:  url('data:application/x-­‐font-­‐ woff;charset=utf-­‐8;base64,...')  format('woff');   }   @font-­‐face  {      font-­‐family:  My  Web  Font;      src:  url('data:application/x-­‐font-­‐ woff;charset=utf-­‐8;base64,...')  format('woff');      font-­‐weight:  700;   }
  35. ASYNC FONTS.CSS Font  Data  URIs   finish   loading B

    A Font  Data  URIs   start   loading REPAINT
  36. @font-­‐face  {      font-­‐family:  Roboto;      src:  url('data:application/x-­‐font-­‐

    woff;charset=utf-­‐8;base64,...')  format('woff');   }   @font-­‐face  {      font-­‐family:  Roboto;      src:  url('data:application/x-­‐font-­‐ woff;charset=utf-­‐8;base64,...')  format('woff');      font-­‐weight:  700;   }
  37. FONTFACEONLOAD github.com/zachleat/fontfaceonload USES THE CSS FONT LOADING API WHEN AVAILABLE.

    FONTFACEOBSERVER github.com/bramstein/fontfaceobserver/ INCLUDES A NICE PROMISES POLYFILL
  38. var  doc  =  document,          docEl  =

     doc.documentElement;   doc.fonts.load("1em  Raleway")    .then(function()  {          //  The  font  has  loaded.      docEl.className  +=  "  raleway-­‐loaded";    }); ‑
  39. body  {      font-­‐family:  sans-­‐serif;   }   .raleway-­‐loaded

     body  {      font-­‐family:  Raleway,  sans-­‐serif;   } ‑
  40. var  docEl  =  document.documentElement;   docEl.className  +=  "  raleway-­‐loading";  

    window.setTimeout(function()  {      docEl.className  +=  "  raleway-­‐fallback";   },  1000);   document.fonts.load("1em  Raleway").then(function()  {      docEl.className  +=  "  raleway-­‐loaded";   });   ‑
  41. .raleway-­‐loading  body  {      color:  transparent;   }  

    .raleway-­‐fallback  body,   .raleway-­‐loaded  body  {      color:  inherit;   }   .raleway-­‐fallback  body  {      font-­‐family:  serif;   }   .raleway-­‐loaded  body  {      font-­‐family:  Raleway,  serif;   } ‑
  42. Promise.all([      document.fonts.load("400  1em  Raleway"),      document.fonts.load("700  1em

     Raleway")   ]).then(function  ()  {      docEl.className  +=  "  raleway-­‐loaded";   }); ‑ document.fonts.load("1em  Raleway").then(function()  {      docEl.className  +=  "  raleway-­‐loaded";   }); BECOMES
  43. THERE ARE A LOT OF TOOLS AT OUR DISPOSAL TO

    EASE THE TRANSITION FROM FALLBACK TO WEBFONT
  44. THE DEFAULT BROWSER BEHAVIOR FOR FONT-LOADING IS HARMFUL TO BOTH

    THE PERCEIVED PERFORMANCE AND INTEGRITY OF CONTENT ON THE WEB. FONT-LOADING INTEGRITY PERCEIVED PERFORMANCE