<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Potato Chip</title>
    <description>The latest articles on DEV Community by Potato Chip (@potatoxchip).</description>
    <link>https://dev.to/potatoxchip</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F735332%2F287b90f8-b249-4e79-9610-bee9af0562d1.png</url>
      <title>DEV Community: Potato Chip</title>
      <link>https://dev.to/potatoxchip</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/potatoxchip"/>
    <language>en</language>
    <item>
      <title>Optimizing fonts for the web</title>
      <dc:creator>Potato Chip</dc:creator>
      <pubDate>Wed, 10 Nov 2021 17:33:43 +0000</pubDate>
      <link>https://dev.to/potatoxchip/optimizing-fonts-for-the-web-2ja5</link>
      <guid>https://dev.to/potatoxchip/optimizing-fonts-for-the-web-2ja5</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This post was originally published on my blog page at &lt;a href="https://try.catch.wtf/blog/optimizing-fonts-for-the-web"&gt;try.catch.wtf&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Adding fonts to your site can make it look better. And they come with some side effects leading to degraded performance. In this post, I will share how to use fonts without taking away a huge bite of your lighthouse scores 😤.&lt;/p&gt;

&lt;p&gt;But before we jump into it, let's analyze some of these side effects.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Delayed text rendering&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a web font has not yet loaded, it can delay text rendering. Resulting in delayed First Contentful Paint (FCP) or even delayed Largest Contentful Paint (LCP) in some cases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Layout shifts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When fonts are loaded and swapped, they can cause layout shifts. These layout shifts occur when a web font and its fallback font take up different amounts of space on the page.&lt;/p&gt;

&lt;p&gt;What can we do to fix these issues 🤔?&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting the font ready to serve
&lt;/h2&gt;

&lt;p&gt;Before we go ahead with anything, let's see how we can optimally serve our font files as fast as possible. If you are using a 3rd party font provider (like Google Fonts), you cannot do much in this case. However, if you are serving the fonts yourself, make sure to serve them (or any static assets) over a CDN and HTTP/2.&lt;/p&gt;

&lt;p&gt;A small font file will always be early to the party 🥳. Downloading multiple font styles (eg. weight, slant) will also hinder the UX. Wouldn't it be great if we could get away without downloading these other styles of the font and use only 1 font to rule them all 😬?&lt;/p&gt;

&lt;h3&gt;
  
  
  Synthetic weights
&lt;/h3&gt;

&lt;p&gt;SHH 🤫! Designers hate this simple trick 🤬.&lt;/p&gt;

&lt;p&gt;Instead of downloading multiple weights of the same font, we can load a regular variant (400 weight) and rely on the browser to synthetically create other weights. If the browser can create synthetic variants, then why do designers hate this trick? It seems to be working perfectly fine, right?&lt;/p&gt;

&lt;p&gt;In the image below, the red text is the bold variant (500 weight) of &lt;a href="https://indestructibletype.com/Jost.html"&gt;Jost*&lt;/a&gt; font, and the blue text is created synthetically from the regular variant (400 weight). The difference might look subtle, but they can impair the UI and UX.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qOnnDWdP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pug9jmgwwps62vvizo2l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qOnnDWdP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pug9jmgwwps62vvizo2l.png" alt="Jost font synthetic 700 weight vs actual 700 weight" width="548" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If this doesn't bother you (or your designers), you can go ahead and use synthetic styles.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variable fonts
&lt;/h3&gt;

&lt;p&gt;A variable font has multiple styles like weight, width, slant, optical size, and italics (called axis) of the fonts. The font creator can create several axes, reducing the number of styles you need to download. For this case, we will use the weight axis. If you want to learn more about the other axes or variable fonts, &lt;a href="https://web.dev/variable-fonts/"&gt;web.dev&lt;/a&gt; has a great article on it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9YxmrgnC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nn5a8a9rvodbife2yhgv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9YxmrgnC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nn5a8a9rvodbife2yhgv.png" alt="Jost variable font 700 weight vs actual 700 weight" width="548" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can use &lt;a href="https://v-fonts.com/"&gt;Variable Fonts&lt;/a&gt; for finding and trying variable fonts&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Font subsetting
&lt;/h3&gt;

&lt;p&gt;Font subsetting is the process of taking a font file and reducing the number of characters (or character sets). For example, let's say you have a font with Japanese characters like &lt;code&gt;月&lt;/code&gt;. You are serving a page that is in English. It is unlikely that you will render Japanese characters. So we can remove them from our font and make a profit!&lt;/p&gt;

&lt;p&gt;Let's subset &lt;code&gt;Jost-400-Book.ttf&lt;/code&gt; (&lt;code&gt;88.7 kb&lt;/code&gt;) from &lt;a href="https://indestructibletype.com/Jost.html"&gt;Jost *&lt;/a&gt; using &lt;a href="https://github.com/zachleat/glyphhanger"&gt;glyphhanger&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# install glyphhanger&lt;/span&gt;
npm i &lt;span class="nt"&gt;-g&lt;/span&gt; glyphhanger

&lt;span class="c"&gt;# install fonttools&lt;/span&gt;
pip &lt;span class="nb"&gt;install &lt;/span&gt;fonttools

&lt;span class="c"&gt;# subsetting Jost-400-Book.ttf font to Latin charset&lt;/span&gt;
glyphhanger &lt;span class="nt"&gt;--LATIN&lt;/span&gt; &lt;span class="nt"&gt;--subset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;Jost-400-Book.ttf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above script results in a &lt;code&gt;Jost-400-Book-subset.ttf&lt;/code&gt; (&lt;code&gt;40.5 kb&lt;/code&gt;) file. Already a reduction of 54% 😱!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are online tools like &lt;a href="https://www.alphabet-type.com/tools/charset-checker"&gt;Charset checker&lt;/a&gt; to check the charset of fonts. You can use this to verify if the subsetted font matches your required charset or not.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  WOFF2 compression
&lt;/h3&gt;

&lt;p&gt;WOFF2 is a compressed font format that can compress a TTF font.&lt;/p&gt;

&lt;p&gt;Let's use our subset font &lt;code&gt;Jost-400-Book-subset.ttf&lt;/code&gt; (&lt;code&gt;40.5 kb&lt;/code&gt;) file and compress it to woff2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# clone woff2&lt;/span&gt;
git clone &lt;span class="nt"&gt;--recursive&lt;/span&gt; https://github.com/google/woff2.git

&lt;span class="c"&gt;# enter into the cloned repo&lt;/span&gt;
&lt;span class="nb"&gt;cd &lt;/span&gt;woff2

&lt;span class="c"&gt;# build&lt;/span&gt;
make clean all

&lt;span class="c"&gt;# convert the font&lt;/span&gt;
./woff2_compress ~/Jost-400-Book-subset.ttf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This results in a &lt;code&gt;Jost-400-Book-subset.woff2&lt;/code&gt; (&lt;code&gt;15.3 kb&lt;/code&gt;) file. A whopping decrease of 62% compared to the subset font (&lt;code&gt;Jost-400-Book-subset.ttf&lt;/code&gt;) and an 82% decrease compared to the original font (&lt;code&gt;Jost-400-Book.ttf&lt;/code&gt;) 😱!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you are using &lt;code&gt;glyphhanger&lt;/code&gt; for font subsetting, you can also compress to woff2 directly in a single command!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;glyphhanger --LATIN --subset=Jost-400-Book.ttf --formats=woff2&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Loading the fonts
&lt;/h2&gt;

&lt;p&gt;Now that we have reduced the font file size, let's see how we can load the font.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@font-face&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Jost&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;400&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;font-display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("/fonts/Jost/Jost-400.woff2")&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;"woff2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.custom-font&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Jost&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This specifies that a font located at &lt;code&gt;/fonts/Jost/Jost-400.woff2&lt;/code&gt;, of &lt;code&gt;woff2&lt;/code&gt; type, with &lt;code&gt;400&lt;/code&gt; weight, and &lt;code&gt;normal&lt;/code&gt; style is referenced as &lt;code&gt;Jost&lt;/code&gt;. Now we can use our font anywhere by setting &lt;code&gt;font-family: Jost;&lt;/code&gt;. Browsers download fonts only if a styling on the page references them. In this case, the browser will only download the Jost font if the page has an element with class &lt;code&gt;.custom-font&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@font-face&lt;/code&gt; rule changes a bit when loading variable fonts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@font-face&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Jost&lt;/span&gt; &lt;span class="n"&gt;VF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300&lt;/span&gt; &lt;span class="m"&gt;800&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* specify weight range of the font */&lt;/span&gt;
  &lt;span class="py"&gt;font-display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;swap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("/fonts/Jost/Jost-VF.woff2")&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;"woff2 supports variations"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="sx"&gt;url("/fonts/Jost/Jost-VF.woff2")&lt;/span&gt;
      &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;"woff2-variations"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.custom-font&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Jost&lt;/span&gt; &lt;span class="n"&gt;VF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;font-display&lt;/code&gt; property tells the browser when to render the font once it is loaded. It accepts the following values:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;auto&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is browser default.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;swap&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The fallback font will be used immediately. Once the custom font downloads, it swaps the font. It can cause 'Flash of Unstyled Text' or &lt;a href="https://css-tricks.com/fout-foit-foft/"&gt;FOUT&lt;/a&gt;. Use &lt;code&gt;swap&lt;/code&gt; only when the font is absolutely necessary. Make sure to deliver the font early enough to prevent layout shifts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zk0EUPM2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1blqs0bq6mabpobsaxb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zk0EUPM2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1blqs0bq6mabpobsaxb.png" alt="Timeline" width="880" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;block&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Displays invisible text for a. Once the custom font downloads, it swaps the font. It can cause 'Flash of Invisible Text' or &lt;a href="https://css-tricks.com/fout-foit-foft/"&gt;FOIT&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XMWmOEPy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zyvz79nb6e2zj1nldr7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XMWmOEPy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zyvz79nb6e2zj1nldr7.png" alt="Success timeline 1" width="880" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MHZ6y6Lp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ht2ib9b2fgrrx26etah8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MHZ6y6Lp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ht2ib9b2fgrrx26etah8.png" alt="Success timeline 2" width="880" height="264"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;fallback&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Blocks rendering for a short time (100ms). If the font is still not downloaded, use the fallback font. Gives a swap period of about 3 seconds for the custom font to load. If it doesn't load within the swap period, it will not be used.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1N9BMiex--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v7pnhddudggjqt4g0rpz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1N9BMiex--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v7pnhddudggjqt4g0rpz.png" alt="Success timeline 1" width="880" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HDuLsYU9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wh4t7ilkthtpx6n6s5kk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HDuLsYU9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wh4t7ilkthtpx6n6s5kk.png" alt="Success timeline 2" width="880" height="264"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kZIZ6RIQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jyqg67n8x05qr7mrot60.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kZIZ6RIQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jyqg67n8x05qr7mrot60.png" alt="Failure timeline" width="880" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;optional&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Like &lt;code&gt;fallback&lt;/code&gt;, it blocks for a while and displays the fallback if the custom font is not yet downloaded. But, the browser decides whether to swap the downloaded custom font depending on the connection speed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preloading?
&lt;/h2&gt;

&lt;p&gt;Sometimes, the absence of fonts can make your page unusable. It might be a no-brainer to load them asap because you know you WILL use them. We can &lt;code&gt;preload&lt;/code&gt; fonts by adding a &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; with &lt;code&gt;preload&lt;/code&gt; hint. This way, instead of the font to be discovered via stylesheet and downloaded, the browser will download the font at the earliest.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Preloaded resources are cached on the browsers for future requests.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
    &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preload"&lt;/span&gt;
    &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/fonts/Jost/Jost-400.woff2"&lt;/span&gt;
    &lt;span class="na"&gt;as=&lt;/span&gt;&lt;span class="s"&gt;"font"&lt;/span&gt;
    &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"font/woff2"&lt;/span&gt;
    &lt;span class="na"&gt;crossorigin&lt;/span&gt;
  &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this comes at a cost 😔. &lt;code&gt;preload&lt;/code&gt;ing resources might obstruct other critical resources for the page. If at all you end up &lt;code&gt;preload&lt;/code&gt;ing fonts, make sure your longest critical chain is short.&lt;/p&gt;

&lt;p&gt;When loading fonts from a 3rd party origin, you can &lt;code&gt;preconnect&lt;/code&gt; to establish an early connection. Below is an example with Google Fonts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- preconnect origin serving stylesheets --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preconnect"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class="c"&gt;&amp;lt;!-- preconnect origin serving fonts --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"preconnect"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.gstatic.com"&lt;/span&gt; &lt;span class="na"&gt;crossorigin&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class="c"&gt;&amp;lt;!-- loading stylesheets --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt;
    &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/css2?family=Jost&amp;amp;display=swap"&lt;/span&gt;
    &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt;
  &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that links with &lt;code&gt;prefetch&lt;/code&gt; and &lt;code&gt;preconnect&lt;/code&gt; hints are executed as the browser sees fit. Whereas &lt;code&gt;preload&lt;/code&gt; is mandatory. Modern web browsers already have good prioritization and do not need &lt;code&gt;preload&lt;/code&gt;s. But if you want some critical resource to be downloaded at the earliest, you can use it sparingly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;preconnect&lt;/code&gt; for fonts needs a &lt;code&gt;crossorigin&lt;/code&gt; attribute because, unlike stylesheets, font files are served over &lt;a href="https://www.w3.org/TR/css-fonts-3/#font-fetching-requirements"&gt;CORS connection&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;How does all this fix the 2 issues we discussed in the beginning?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster text rendering&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A font will render faster if it is downloaded faster. Use variable fonts when needing multiple font styles. Subset and convert fonts to woff2 for the smallest font file size. Serve them over a CDN and HTTP/2 protocol for the fastest and most reliable speeds. &lt;code&gt;preload&lt;/code&gt; fonts when serving them or &lt;code&gt;preconnect&lt;/code&gt; fonts/stylesheets when using 3rd party services. When preloading, make sure your longest critical chain is the smallest possible&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Layout shifts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the custom font is not a top priority, use &lt;code&gt;font-display: optional&lt;/code&gt;. This guarantees no layout shifts. But if the custom font is a top priority, use &lt;code&gt;font-display: swap&lt;/code&gt; with the above optimizations for the font delivery.&lt;/p&gt;

&lt;p&gt;Hope you find this post helpful! SA-YO-NA-RA! 👋😽&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>frontend</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
