Skip to main content

Command Palette

Search for a command to run...

Reduce Google Fonts file size by more than 80%

Published
β€’2 min read
Reduce Google Fonts file size by more than 80%
D

I love the web πŸ•Έ

I also write about personal finance in Spanish at https://perrodinero.blog

Sometimes we only need a font for a couple of words, like a logo or slogan. Why do we download the whole font file if we only need a few letters?

In this post, I'll show you how you can reduce your font file size by more than 80%. This size reduction will be more effective the fewer characters you need.

Base HTML

This is the base HTML we'll use. It contains the Roboto font and two headers. One with Roboto font-family and the other with the default font:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="https://fonts.googleapis.com/css2?family=Roboto" rel="stylesheet">

</head>
<body>
  <h1 style="font-family: 'Roboto', sans-serif;">David Ojeda</h1>
  <h2>Default font</h2>
</body>
</html>

Measuring current font file size

If you open this file with your browser and open the network tab of the Developer Console, you would see this:

Developer console "Network" tab for previous HTML

~657 bytes for the Google Fonts API call and ~11.1 kB for the actual Roboto font.

If you look at the HTML again, you can notice that we're only using the font for my name, David Ojeda. What if we could download only those letters? It turns out we can.

Changing the font URL

We need to change the font URL from this:

<link href="https://fonts.googleapis.com/css2?family=Roboto" rel="stylesheet">

to this:

<link href="https://fonts.googleapis.com/css2?family=Roboto&text=David%20Ojeda" rel="stylesheet">

What's new? The &text= URL parameter. We'll only download the characters we need. Just remember, the text parameter value:

  • Is case-sensitive.
  • Needs to be URL encoded.

Measuring updated font file size

Let's measure the size of the file now:

Developer console "Network" tab for updated HTML

~359 bytes for the Google Fonts API call and ~1.3 kB for the actual Roboto font. That's like 89% file reduction for the font!

Wrap up

I found this implementation in the Google Fonts documentation days ago, and I wanted to share it so more people know about it.

It saved me this 80-90% file size on my personal finance blog, and I'm sure it can help others do the same.

Thanks for reading me! πŸ’™

S

I was just picking out custom fonts for a project. I'll try this update next time I'm working on it.

1
K
Kishore5y ago

This is one of the best hack I read.

1
D

For popular fonts this will likely be slower and use more network access.

If they have already visited a site that uses the font you use by putting ?text= on the end you're forcing the browser to go off to Google to get your subset even though the full font file is sitting their in their browsers disk cache.

1
N

This is outdated wisdom, due to security issues:

https://www.chromestatus.com/feature/5730772021411840 https://bugzilla.mozilla.org/show_bug.cgi?id=1536058 https://bugs.webkit.org/show_bug.cgi?id=110269

Using the advice in the article is just fine.

1
D

NostraDavid Indeed, looks like they went ahead and chose sidechannel leaking privacy over perf. If you follow a lot of the discussions they claim there's little to no perf benefit from using caches.

I wonder how many of their tests and experiences were on fat company pipes and gigabit home internet connections... :(

1
D

Thanks to both of you for the discussion!

So, cache works or don't? πŸ€”

Developer Console network tab with size column highlighted showing files served from cache

N

David Ojeda From what I understand: Cache works, but if I go to example.com and load a woff font, I will have to redownload it for your site (doesn't matter if the URL is the same).

1
D

David Ojeda The cache is no longer shared between origins so there is literally no benefit to serving up from googlewebfonts, bootstrapcdn etc. any more.

If you have your own private CDN like Amazon CloudFront you may as well just copy the assets there now and be in full control.

1
D

NostraDavid Understood!

D

Damien Guard Got it. Good to know!

J

Lovely. I want to add that you can do the same thing with fonts you have bought somewhere else with a free tool like fontforge. I recommend this to everyone who has a high-traffic landing page. It’s pure money!

1
D

Thanks for the extra tip!

K

Nice, but IΒ΄d make an SVG if it is only for some letters.

2
D

That can work too!

S

Wow! Awesome tip. Added to my TIL. πŸ™Œ

1
D

Thanks! Glad you learned something with my post πŸ™ŒπŸΌ

T

David Ojeda, This is a great tip. Thanks for the write-up.

1
D

Thanks to you for the Bootcamp session, Tapas!

1