Benjamin Looi
Back to blogs
Google fonts not loaded in generated static Nuxt app

Google fonts not loaded in generated static Nuxt app

Benjamin Looi / May 16, 2021

My static Nuxt sites on Netlify were not using the Google Fonts I specified, even though the fonts worked during development.

assets/css/main.scss
/* assets/css/main.scss */
@import url("https://fonts.googleapis.com/css2?family=Archivo:wght@400;800&display=swap");
 
/* ... */
nuxt.config.js
// nuxt.config.js
// ...
css: [
    // https://eduardoboucas.github.io/include-media/
    // 'include-media/dist/_include-media.scss',
    // Global custom scss
    '@/assets/css/main.scss',
  ],
// ...

Moving the @import CSS into a top-level Vue component did not fix it.

pages/index.vue
// pages/index.vue
<style lang="scss" scoped>
@import url("https://fonts.googleapis.com/css?family=Playfair+Display:400,700,900&display=swap");
 
/* ... */
</style>

Hypothesis 1

Tailwindcss is purging the @import css

Solution?

I tried disabling Tailwind CSS purging.

tailwind.config.js
// tailwind.config.js
module.exports = {
  purge: {
    enabled: false,
  },
  // ...
}

The font still did not load on the generated static site.

Final Solution

Include the font as a stylesheet link in the HTML head instead of using @import CSS.

nuxt.config.js
// nuxt.config.js
export default {
  // ...
  head: {
    // ...
    link: [
      // ...
      {
        rel: "stylesheet",
        href:
          "https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@300;400;500;600;700&display=swap",
      },
    ],
  },
  // ...
}
Thanks for reading! 😁

Comments

Related Posts

Related Projects

extension
Just a Calculator

Just a Calculator

A satirical browser extension built with WXT and React 19 that rewrites AI hype back to reality, one text node at a time.

wxt
react
typescript
vanilla css
web
Open Resume

Open Resume

A resume builder with AI coaching, server-side rendering, local state persistence, and Cloudflare Workers deployment.

react
typescript
tailwind
zustand