How to Efficiently Load Google Fonts with Multiple Weights in a Nuxt Project?

Mary-Kate Olsen
Release: 2024-10-31 12:31:30
Original
697 people have browsed it

How to Efficiently Load Google Fonts with Multiple Weights in a Nuxt Project?

Efficient Loading of Google Fonts in Nuxt

You're encountering issues applying different font weights to your Google fonts in a Nuxt project. The problem arises from importing multiple font links for the same font with varying weights, which introduces redundancy.

Recommendation:

The optimal solution is to avoid using a CDN for font loading. Self-hosting or proxying Google fonts through the page origin is a more efficient approach. To facilitate this, consider utilizing the @nuxtjs/google-fonts module.

Module Configuration:

In your nuxt.config.js file, add the following code:

<code class="js">export default {
  buildModules: [
    [
      '@nuxtjs/google-fonts',
      {
        families: {
          'Saira Semi Condensed': {
            wght: [600, 800],
          },
        },
        subsets: ['latin'],
        display: 'swap',
      },
    ],
  ]
}</code>
Copy after login

This configuration specifies that both font weights (600 and 800) should be loaded for the 'Saira Semi Condensed' font family.

CSS Configuration:

In your Layout.vue, import the font family once:

<code class="css">@import url('https://fonts.googleapis.com/css2?family=Saira+Semi+Condensed');</code>
Copy after login

Ensure you also add CSS rules to apply the specific font weights in the respective components:

ComponentA.vue:

<code class="css">footer {
  font-weight: 600;
}</code>
Copy after login

ComponentB.vue:

<code class="css">footer {
  font-weight: 800;
}</code>
Copy after login

Note:

If specific font weights fail to download, update the @nuxtjs/google-fonts package to v3.0.0-1 or set overwriting: true in the module configuration.

The above is the detailed content of How to Efficiently Load Google Fonts with Multiple Weights in a Nuxt Project?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!