<?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: Arya Dhiratara</title>
    <description>The latest articles on DEV Community by Arya Dhiratara (@aryadhiratara).</description>
    <link>https://dev.to/aryadhiratara</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%2F825205%2F8041a152-b6f4-442d-acbd-33e22916c65e.jpg</url>
      <title>DEV Community: Arya Dhiratara</title>
      <link>https://dev.to/aryadhiratara</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aryadhiratara"/>
    <language>en</language>
    <item>
      <title>Optimize Fonts to Speed Up Your Website</title>
      <dc:creator>Arya Dhiratara</dc:creator>
      <pubDate>Fri, 04 Mar 2022 09:25:09 +0000</pubDate>
      <link>https://dev.to/aryadhiratara/optimize-fonts-to-speed-up-your-website-7ma</link>
      <guid>https://dev.to/aryadhiratara/optimize-fonts-to-speed-up-your-website-7ma</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;One of the main keys of optimizing or speeding up website performance is reducing the total requests and file size in a page. That will include Fonts. – &lt;a href="https://thinkweb.dev/"&gt;ThinkWeb&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Fonts are crucial to our page design. Using different font types can change the whole design character of our web page. Choosing the right font is as important as choosing the right design.&lt;/p&gt;

&lt;p&gt;Unfortunately, fonts can make our site slow. Web designers often use multiple fonts just to support the character of their designs. Using multiple fonts means multiple file requests. This can hurt our website performance.&lt;/p&gt;

&lt;p&gt;So, to ensure that we'll pass the &lt;a href="https://thinkweb.dev/how-to/optimize-core-web-vitals/"&gt;Core Web Vitals&lt;/a&gt; score, fonts are also important to optimize.&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimizing Google Fonts
&lt;/h2&gt;

&lt;p&gt;The most common method that people often use is by embedding fonts from the Google Fonts library. Yes, this is the easiest way for using fonts on our website. But it also has its drawbacks.&lt;/p&gt;

&lt;p&gt;Serving fonts from google can actually reduce the speed performance of our website if we're not using it carefully and handling it properly.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There are so many unused bytes in their default CSS (Google Fonts CSS)&lt;/li&gt;
&lt;li&gt;The amount of time to make the connections and fetch files from 2 different DNS resources can often make a huge render blocking time, especially if we have a large page size and uses so many assets.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both may cost us warnings on our Core Web Vitals scores.&lt;/p&gt;

&lt;p&gt;So, how can we optimize them?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Self Host the Google Fonts CSS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The default method to embeds font(s) from the Google Fonts library is by using link tag like this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,700;1,400;1,700&amp;amp;display=swap" rel="stylesheet"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we open that url, we will find so many fonts css for multiple language. While all we need usually is just the Latin text only. By self-hosting the fonts css and select the latin fonts only, we can reduce the CSS size significantly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Inline the Google Fonts CSS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As fonts are a part of the critical assets, it is best to inline the CSS in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; section to eliminate the need to fetch the style from the CSS file.&lt;/p&gt;

&lt;p&gt;Here's an example for Roboto:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;style&amp;gt;
@font-face {
   font-family: 'Roboto';
   font-style: normal;
   font-weight: 400;
   font-display: swap;
   src: url(https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4mxKKTU1Kg.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;
 }
&amp;lt;/style&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Limit the Font Families&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The font's file size isn't small, even if we use the woff2 extension. These are the woff2 file size of the 3 most frequently used Google Fonts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4cAqIxJo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8kg4gv8flhp50vpygw8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4cAqIxJo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8kg4gv8flhp50vpygw8.jpg" alt="Fonts file size comparison" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, even if we 'only' use 2 font combinations, the browser must download additional files of at least 20KB (gzipped) with an additional 4 font requests. And it will be more if we use more fonts.&lt;br&gt;
Remember, all file requests are render-blocking. So we really should try to minimize the number of requests as much as possible.&lt;br&gt;
Best Practice: Don't use more than 2 fonts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Limit the Font Variations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In most cases, actually we don't need to embed the font files for bold and italic style, especially if we use the Sans-Serif Fonts families. The browser will still tilt and adjust the thickness if there's a &lt;code&gt;&amp;lt;strong&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;em&amp;gt;&lt;/code&gt; tag as well as the font-weight: 700, font-weight: bold, and font-style, in the text CSS.&lt;br&gt;
The results are often quite the same. Or at least, can be tolerated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ubtr6td1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kb1yll6gttm0qcyce6sd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ubtr6td1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kb1yll6gttm0qcyce6sd.jpg" alt="Roboto font comparison" width="800" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Filter the Characters&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2vBfT6J4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2aw4l1aq3sux2ya85q40.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2vBfT6J4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2aw4l1aq3sux2ya85q40.jpg" alt="Roboto font characters" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image above shows the types of characters in a regular Roboto font file. We don't actually need all of them, right?&lt;br&gt;
If we only need the latin characters, we can filter the characters that we need from Google Fonts with something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&amp;amp;text=1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@!-_+/\|/?%20&amp;amp;subset=latin&amp;amp;display=swap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*change the font name and the weight to everything that you need.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Self Host the Font Files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Self hosting our font files will always be faster than using the files served by Google Fonts CDN in terms of performance.&lt;br&gt;
Here's the easiest way to do it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, download the font file(s) directly using the font source url in the Google Fonts CSS, or use the &lt;a href="https://google-webfonts-helper.herokuapp.com/fonts"&gt;Google-WebFonts-Helper&lt;/a&gt; created by Mario Rantfl.&lt;/li&gt;
&lt;li&gt;Then upload the file to our host using FTP or cPanel's File Manager.&lt;/li&gt;
&lt;li&gt;Finally, call the file via CSS.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@font-face {
   font-family: 'Roboto';
   font-style: normal;
   font-weight: 400;
   font-display: swap;
   src: url(/font-url-path/font-file-name.woff2) format('woff2');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;7. Preload the Critical Font Files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Preload is useful for asking the browser to fetch the font file(s) right after the browser finish rendering the HTML, before fetching anything else.&lt;br&gt;
We just need to put this in our &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Roboto Regular 400 */
&amp;lt;link rel="preload" href="https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4mxKKTU1Kg.woff2" as="font" crossorigin&amp;gt;

/* Roboto Bold 700 */
&amp;lt;link rel="preload" href="https://fonts.gstatic.com/s/roboto/v20/KFOlCnqEu92Fr1MmWUlfBBc4AMP6lQ.woff2" as="font" crossorigin&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It should helps us avoid FOIT (Flash of Invisible Text), FOUT (Flash of Unstyled Text), and additional CLS (Cumulative Layout Shift).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BONUS: Convert the Font File to Base64&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can eliminate the need to fetch the font files by converting them into Base64 encoded string.&lt;br&gt;
Just upload the files to Base64 online converters like &lt;a href="https://www.giftofspeed.com/base64-encoder/"&gt;Gift Of Speed&lt;/a&gt;, then copy and paste the base64 encoded string as the font's url source. The CSS code should be like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@font-face {
   font-family: 'Roboto';
   font-style: normal;
   font-weight: 400;
   font-display: swap;
   src: url(data:font/woff2;base64,OUR-FONT-BASE64-CODES;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Important: A base64 encoded font string size are larger than the font file size itself. So it will be best to have it filtered first.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Hope this helps. You can read the original article &lt;a href="https://thinkweb.dev/how-to/optimize-fonts/"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>font</category>
      <category>speed</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
