iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👋

Exploring Next.js Font Optimizations: Web Font Optimization Guide

に公開
2021/04/29

Update) Google Fonts Optimization is enabled by default since v10.2

Starting from Next.js v10.2, Web font optimization is now performed automatically. As of v10.2, it is stated that only Google Fonts are supported. No special configuration is required; you can simply load Google Fonts as usual.

↓ Details

https://nextjs.org/blog/next-10-2#automatic-webfont-optimization

The explanation below is old content. Please do not use it as a reference for implementation.


Previously, while browsing the Next.js repository, I found a pull request titled Font Optimizations.

As of December 2020, it seems to be an experimental feature, but since it was already available in v10.0.4, I decided to give it a try.

What is Font Optimization?

Since documentation has not been written yet, I will quote the description from the pull request.

...omitted... we will grab the <link rel='stylesheet' href='https://fonts.googleapis.com/...'/>(List of font providers to be extended in future PRs) tags and inline it's content.

It seems to find Web font stylesheets within the outputted HTML and inline their content. For now, it appears to support Google Fonts.

This eliminates the extra round trip that the browser has to make to fetch the font declarations. This helps in faster paints and improved LCP.

To put it very simply, it saves time on fetching font declarations, which is expected to speed up the display. The "LCP" mentioned in the text above stands for Largest Contentful Paint.

How is it achieved?

Below is also a quote from the pull request.

  1. Gathering the used <link rel='stylesheet' href='https://fonts.googleapis.com/...'/> tags and downloading their content at build time.
  2. Replacing the inspected Link tag with a style tag and the content of the href inlined within this style tag at serve/render time.
    In order to do the above-said we use.

At build time, it seems to find stylesheet loading tags starting with https://fonts.googleapis.com/... and download all of their content. It's great because self-hosting is generally considered to have better performance than loading Web fonts from a CDN.

Reference

However, as I found out by actually trying it, the font data itself (◯◯.woff) still seems to be loaded from Google's servers even when using Font Optimization. I wonder if they will download woff files as well in the future?

For a more detailed mechanism of Font Optimization, it's best to read the pull request.

Trying it out in practice

Loading Google Fonts in Head

Now, let's actually try it. Looking at the code, it seems that <link> tags placed inside next/head are the targets. So, we will load Google Fonts in _app.tsx.

_app.tsx
import Head from "next/head"

...omitted
return (
  <Head>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@700&display=swap" />
  </Head>
)

Enabling Font Optimization in next.config.js

As of v10.0.4, it needs to be specified within experimental.

next.config.js
module.exports = {
  experimental: {
    optimizeFonts: true,
  },
};

That's all it takes to be ready.

Deploying and Verifying

After deploying, it seems that Google Fonts are correctly applied. If you look at the source, you can see that the Google Fonts <link> tag has become data-href instead of href.

Also, the content of data-href (= the content of https://fonts.googleapis.com/...) is expanded into a style tag.

As you can see from the image above, the font data itself is being fetched from https://fonts.gstatic.com. This is the same as when loading Google Fonts normally.

What are the effects of Font Optimization?

I measured it with Lighthouse. I thought it wouldn't change much...

Without Font Optimization

With Font Optimization

What...?
Does it change that much?

"First Contentful Paint" and "Total Blocking Time" have been reduced by nearly 1 second. When I load it with the browser cache disabled, it indeed feels a bit faster in terms of perceived speed...?

Conclusion

I think Font Optimization is a very beneficial feature. It would be even better if .woff data itself could be self-hosted without any configuration.
(Although deploying to Vercel might consume a lot of bandwidth, potentially requiring a plan upgrade.)

By the way,
Font Optimization is already enabled on Zenn, which you are viewing right now.

Discussion