How to efficiently load google fonts in Nuxt
You're loading your fonts from a CDN, which is not the recommended way of doing things.
Here is a quote from this awesome performance checklist 2021 written by Vitaly Friedman
Now, many of us might be using a CDN or a third-party host to load web fonts from. In general, it’s always better to self-host all your static assets if you can, so consider using google-webfonts-helper, a hassle-free way to self-host Google Fonts. And if it’s not possible, you can perhaps proxy the Google Font files through the page origin.
Looking at this, I do recommend the usage of @nuxtjs/google-fonts
, this is a cool Nuxt module.
I've actually asked if my configuration of the module was okay, here is the github issue: https://github.com/nuxt-community/google-fonts-module/issues/26
And here, a usage example in nuxt.config.js
export default {
buildModules: [
[
'@nuxtjs/google-fonts',
{
families: {
Mali: {
wght: [400, 600, 700],
},
},
subsets: ['latin'],
display: 'swap',
prefetch: false,
preconnect: false,
preload: false,
download: true,
base64: false,
},
],
]
}
How to link a Google font to my Nuxt files?
If you want to have your fonts loaded properly, you need to have them linked to your page somehow at some point. Hence why putting your SCSS files into the css
property is probably the way to go.
nuxt.config.js
export default {
// Global CSS: https://go.nuxtjs.dev/config-css
css: ['put-your-files-here']
}
Otherwise, here is how to load some fonts properly with Nuxt.
Google font import not working with NuxtJS component style
Try to follow my answer here and set the default font of Vuetify to use this one.
Also, please use the related google-fonts-module module to have them done at build time globally rather than scoped locally per component (CDN is usually not reliant/performing poorly).
How to host google fonts locally in Nuxt
this question was asked not so long ago. Here is my solution: https://stackoverflow.com/a/68166329/8816585
Give it a try, this is using @nuxtjs/google-fonts
for some simple local fonts fetching during build time and works pretty well!
Loading custom fonts in Nuxt/Tailwind Project
I'm assuming you're using the module @nuxtjs/tailwindcss.
First, you'll have to run
npm run build
, so that tailwind files are created:- ~/tailwind.config.js
- ~/assets/css/tailwind.css
Create a folder
fonts
underassets
and place the fonts you've downloaded.Include your fonts in
~/css/tailwind.css
, as such:
@include font-face( KapraNeuePro, '~/assets/fonts/KapraNeueProFamily/Kapra-Neue-Pro-Regular', 400, normal, otf);
@include font-face( KapraNeuePro, '~/assets/fonts/KapraNeueProFamily/Kapra-Neue-Pro-Medium', 600, medium, otf);
etc.
- Check out tailwind's docs on how to add font families in
tailwind.config.js
, and which configuration better suits your needs:
(the following one is a quick working example)
module.exports = {
theme: {
fontFamily: {
sans: ["KapraNeuePro"],
serif: ["KapraNeuePro"],
mono: ["KapraNeuePro"],
display: ["KapraNeuePro"],
body: ["KapraNeuePro"]
},
variants: {},
plugins: []
}
};
- Dont' forget to remove from your layout and page all the default CSS related to
font-family
Related Topics
Shorten Verbose CSS That Repeats Combinations of Elements and Pseudo-Classes
How to Use 3-Digit Color Codes Rather Than 6-Digit Color Codes in CSS
Use Custom Fonts with Wkhtmltopdf
Css3 Transitions on Pseudo-Elements (:After, :Before) Not Working
Using Modulus in CSS Calc Function
Highcharts Graph Width Is Incorrect When Scrollbar Is Present, in Bootstrap Fluid Span
Export CSS Changes from Inspector (Webkit, Firebug, etc)
@Font-Face Failed Opentype Embedding Permission Check. Permission Must Be Installable
Why Does Bootstrap 3 Force the Container Width to Certain Sizes
Looping Animation of Text Color Change Using CSS3
CSS to Make Bootstrap Navbar Transparent
Fix Custom Font Line-Height with CSS
Css: Background-Color on Multi-Line Text
Are All CSS Font-Weight Property's Values Useful