Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nuxt.js - preload .woff fonts loaded as @font-face

Trying to improve my google score and google is telling me to use preload on the two custom fonts i'm using to save a whopping 4.5 seconds? currently the fonts are stored in assets/fonts and then being loaded as @font-face in typography.scss file when is then loaded in the nuxt.config.js file inside css: [ '@/assets/scss/typography.scss', ]

Image Preview

like image 696
user10649239 Avatar asked Jan 08 '19 00:01

user10649239


1 Answers

So I guess you are asking how to preload a font? There is a way to call a render function in nuxt.config.js that will preload fonts, and scripts and styles, and then have them available in the client so you do not have to load the font in your scss file, just set it. Try this:

//nuxt.config.js

module.exports = {
  mode: ' your mode ',

  ...

  render: {
    bundleRenderer: {
      shouldPreload: (file, type) => {
        return ['script', 'style', 'font'].includes(type)
      }
    }

  },
  ...

}

You should also probably store your fonts in the static folder. /static/fonts/yourfonts.woff2

like image 65
Andrew1325 Avatar answered Sep 27 '22 22:09

Andrew1325