Simple, modern and lightweight font loader for Nuxt projects
all
types of font loading 🔥 (local, Google, Typekit, custom, etc.)modern
, fast
and efficient
font loadingprefetch
, preconnect
and preload
dev
and prod
mode (supports SPA & SSR)url
optionmultiple
font sources at the same timecustomizable
settings for advanced usagenuxt-font-loader
dependency to your project$ npm install --save-dev nuxt-font-loader
nuxt-font-loader
to the buildModules
section of nuxt.config.js
// nuxt.config.js
export default {
buildModules: ['nuxt-font-loader'],
fontLoader: {
/* module options */
}
}
Basic usage
// nuxt.config.js
{
fontLoader: {
url: '/fonts/font-face.css' // Path to your css file
}
}
font-face.css
/* Define @font-face rules */
@font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 300;
font-display: swap;
src: url('/fonts/I-300.woff2') format('woff2');
/* Consider adding a 'unicode-range' CSS descriptor */
}
@font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url('/fonts/I-400.woff2') format('woff2');
/* Consider adding a 'unicode-range' CSS descriptor */
}
Specify families
/* Specify the font-family as usual */
html {
font-family: 'Inter', sans-serif;
}
Basic usage
// nuxt.config.js
{
fontLoader: {
// Paste a google link here
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap',
// Enable options
prefetch: true,
preconnect: true
}
}
Specify families
/* Specify the font family as usual */
html {
font-family: 'Roboto', sans-serif;
}
Basic usage
// nuxt.config.js
{
fontLoader: {
// Paste a new custom link here (for example Typekit)
url: 'https://new-custom-link/',
// Enable options
prefetch: true,
preconnect: true
}
}
Specify families
/* Specify the font family as usual */
html {
font-family: 'New Custom Family', sans-serif;
}
Automatically sets the best settings based on your url
option
// nuxt.config.js
{
fontLoader: {
url: {
local: '/fonts/font-face.css',
google: 'https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap',
custom: 'https://new-custom-link/'
}
}
}
Use this method if you want to load multiple font sources at the same time
Specify families
/* Specify the font family as usual */
html {
font-family: 'Inter', sans-serif; /* Local */
}
nav {
font-family: 'Roboto', sans-serif; /* Google */
}
h1 {
font-family: 'New Custom Family', sans-serif; /* Custom */
}
Use these methods only if you want to customize the default
settings
// nuxt.config.js
{
fontLoader: {
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap',
prefetch: {
hid: 'my-font-prefetch',
},
preconnect: {
hid: 'my-font-preconnect',
crossorigin: 'anonymous',
},
preload: {
hid: 'my-font-preload',
},
noscript: {
hid: 'my-font-noscript',
}
}
}
// nuxt.config.js
{
fontLoader: {
url: {
local: '/fonts/font-face.css'
},
preload: {
local: {
hid: 'my-font-preload'
}
},
noscript: {
local: {
hid: 'my-font-noscript'
}
}
}
}
Following these examples, it is possible to customize all settings as needed
Default options
// nuxt.config.js
{
fontLoader: {
url: {
local: undefined,
google: undefined,
custom: undefined,
},
prefetch: false,
preconnect: false,
preload: {},
noscript: {},
stylesheet: true,
}
}
url
{}
Defines the path of the css file that includes all @font-face rules.
This option is required.
prefetch
false
Enable this if you request fonts from a third-party server, such as Google, Typekit, etc.
When used with multiple sources method, this is enabled by default
<link rel="dns-prefetch" href="https://fonts.gstatic.com/" />
preconnect
false
Enable this if you request fonts from a third-party server, such as Google, Typekit, etc.
When used with multiple sources method, this is enabled by default
<link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin />
preload
{}
Preloads a css file to increase its priority.
<link rel="preload" as="style" href="/path/to/font-face.css" />
noscript
{}
Provides a fallback option in case the user disables javascript.
<noscript><link rel="stylesheet" href="/path/to/font-face.css" /></noscript>
stylesheet
true
Eliminates render-blocking effect and improves site performance by loading the font css asynchronously.
<link rel="stylesheet" href="/path/to/font-face.css" />
Author: ivodolenc
Source Code: https://github.com/ivodolenc/nuxt-font-loader
#vue #vuejs #javascript #nuxtjs