iTranslated by AI

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

Speed Up Web Font Loading in Nuxt.js with @nuxtjs/google-fonts

に公開

The other day, this article was trending, and I looked into whether Nuxt.js had a similar feature and found it.

https://zenn.dev/catnose99/articles/bb943c3dc99d89

@nuxtjs/google-fonts

https://github.com/nuxt-community/google-fonts-module

@nuxtjs/google-fonts is a Nuxt.js module for Google Fonts. This module has options called download and inject, and by enabling these, you can achieve the same result as Next.js Font Optimizations.

Actually tried it

Here is the repository I used for testing.
https://github.com/odan-sandbox/nuxt-font-optimization-sandbox

I added the following settings to nuxt.config.js.

nuxt.config.js
  buildModules: [
    ['@nuxtjs/google-fonts', { families: { Inter: true }, display: 'block', download: true, inject: true }]
  ],

When you run nuxt build/generate in this state, font files and a CSS file that loads them are generated locally.

If the inject option is enabled, the generated CSS file is automatically loaded as a global CSS.

Conclusion

When I saw the original Next.js article, I thought about making a similar feature myself if Nuxt.js didn't have one, but it already existed.
While Next.js uses a mechanism to analyze HTML and automatically replace things, Nuxt.js provides it as a module with an option to handle inline expansion, showing a difference in philosophy. Personally, I prefer the Nuxt.js approach as it is easier to understand.

Discussion