
Nuxt Fonts
Plug-and-play custom web font optimization and configuration for Nuxt apps.
Features
- ✨ zero-configuration required
- 🔡 built-in providers (
google
, bunny
, fontshare
, local
- more welcome!) - 💪 custom providers for full control
- ⏬ local download support (until
nuxt/assets
lands) - ⚡️ automatic font metric optimisation powered by fontaine and capsize
- 🔥 build/dev time font caching powered by unstorage
👉 See Nuxt Fonts RFC for full details and discussion.
Quick Start
[!IMPORTANT]
@nuxt/fonts
is not yet released and the below step will not yet work. Instead you can manually install @nuxt/fonts-nightly
.
pnpm i @nuxt/fonts@npm:@nuxt/fonts-nightly
To get started, simply run:
npx nuxi@latest module add fonts
If you don't already have it in your .gitignore
, go ahead and add the .data
directory:
.data
Then just add a font-family
declaration in your CSS:
<template>
<div>
Hello Nuxt Fonts!
</div>
</template>
<style scoped>
div {
font-family: Roboto, sans-serif;
}
</style>
That's it! Nuxt Fonts will detect this and you should immediately see the web font loaded in your browser. Read more about how it works.
[!TIP]
Even if you're using a preprocessor like TailwindCSS or UnoCSS, Nuxt Fonts should be able to detect and optimize your fonts with no configuration.
Configuration
You do not need to configure Nuxt Fonts but you can do so for finer-grained control.
export default defineNuxtConfig({
modules: ['@nuxt/fonts'],
fonts: {
families: [
{ name: 'Custom Font', provider: 'none' },
{ name: 'My Font Family', provider: 'google' },
{ name: 'Other Font', src: 'url(https://example.com/font.woff2)', weight: 'bold' },
],
defaults: {
weights: [400],
styles: ['normal', 'italic'],
subsets: [
'cyrillic-ext',
'cyrillic',
'greek-ext',
'greek',
'vietnamese',
'latin-ext',
'latin',
]
},
fallbacks: {
'serif': ['Times New Roman'],
'sans-serif': ['Arial'],
'monospace': ['Courier New'],
},
assets: {
prefix: '/_fonts'
},
providers: {
custom: '~/providers/custom',
google: false,
},
}
})
How it works
Nuxt Fonts processes all your CSS and does the following things automatically when it encounters a font-family
declaration.
-
Resolves fonts used in CSS. It starts by looking in your public/
directory for font files that match the name, like Roboto.woff2
, RobotoBold.ttf
, etc. Then it moves on to web font providers like google
, bunny
and fontshare
. Once a provider is found (in this case, probably Google Fonts), we move on to the next step.
-
Generates and injects @font-face
rules for you. We'll generate rules to point your browser to the right source files. They'll be injected into the same CSS where you use the font-family
.
:root {
font-family: Poppins;
}
@font-face {
font-family: 'Poppins';
src: local("Poppins"), url("/_fonts/<hash>.woff2") format(woff2);
font-display: swap;
unicode-range: U+0000-00FF,U+0131, ;
font-weight: 400;
font-style: normal;
}
-
Proxies and caches font requests. Rather than using the original source URLs (to remote servers), we generate rewrites under the /_fonts
subpath. When accessed by your browser, we download the font from the remote server and cache it locally.
-
Creates font fallback metrics. If we have access to the font metrics (ascent, descent, line gap, character width, etc.) then we can generate a fallback @font-face
declaration as well. The idea is that we 'morph' a local system font (like Arial or Times New Roman) to be as close as possible to the size of the web font, to decrease layout shift (read more about CLS).
:root {
font-family: Roboto, Helvetica, Arial;
font-family: Merriweather, serif;
}
-
Include fonts in build. When you build your project, we'll copy across all the fonts used in your project so you don't need to make any external requests when loading your site. (Any that haven't already been cached in development are downloaded at build time.) Font file names are hashed and Nuxt will serve them with long-lived cache headers.
Font providers
Font providers are designed to be pluggable and extensible, so no matter your setup you should be able to use an existing provider or write your own, and still benefit from core functionality of Nuxt Fonts.
We ship with four built-in providers.
local
The local provider deeply scans your public/
directories (including of your layers) for font files (supporting ttf, woff, woff2, eot or otf extensions).
Then, when you use a font-family
in your CSS, we check to see whether it matches one of these files. We also expect font weight, font style and subset to be in the file name, unless they are 'default' values (400
weight, normal
style and latin
subset).
google
Google Fonts is one of the best known public font APIs.
bunny
Bunny Fonts is provided by bunny.net and is a drop-in Google Fonts compatible API, focusing on privacy.
fontshare
Fontshare is a free font service with 100+ professional-grade fonts from the Indian Type Foundry (ITF).
You should read their terms in full before using a font through fontshare
.
Writing a custom provider
The provider API is likely to evolve in the next few releases of Nuxt Fonts, but at the moment it looks like this:
import { defineFontProvider } from '@nuxt/fonts/utils'
export default defineFontProvider({
async setup () {
},
async resolveFontFaces (fontFamily, defaults) {
if (fontFamily === 'My Font Family') {
return {
fonts: [
{
src: [
{ url: 'https://cdn.org/my-font.woff2', format: 'woff2' },
'https://cdn.org/my-font.woff',
],
weight: 400,
style: 'normal',
}
]
}
}
}
})
Module authors can also add their own providers (or remove existing ones) in the fonts:providers
hook which is called by Nuxt Fonts after all modules have run.
nuxt.hook('fonts:providers', providers => {
providers.push({
async setup () {
},
async resolveFontFaces (fontFamily, defaults) {
}
})
})
Contributing
- Clone this repository
- Enable Corepack using
corepack enable
- Install dependencies using
pnpm install
- Run
pnpm dev:prepare
to generate type stubs. - Use
pnpm dev
to start playground in development mode.
📑 License
Published under the MIT License