Optimize your site's speed with Nuxt Fontaine—reducing CLS and automating font fallbacks for peak performance. Elevate your web experience effortlessly.
In the realm of web development, the quest for optimal performance often leads us to explore intricate solutions for backend optimization. However, the frontend, often underestimated in its impact, plays a crucial role in elevating user experiences. In a previous article, I delved into improving Nuxt performance with PartyTown, enabling the execution of third-party scripts like Google Analytics as web workers.
Today, our focus shifts to a fundamental yet powerful aspect—fonts. More specifically, we'll dive into the nuanced concept of font face override, facilitated by a tool called Nuxt Fontaine.
Nuxt Fontaine stands as a font metric override tool tailored for Nuxt 3. As we embark on this journey, we'll explore its features, installation process, usage guidelines, and the underlying mechanisms that contribute to an enhanced web performance.
⚠️ Note: @nuxtjs/fontaine
is currently under active development. ⚠️
In our exploration, we'll witness the impact on CLS and overall performance, illustrating the substantial improvements achieved effortlessly.
While Fontaine brings significant advantages, it's essential to note that, for optimal performance, inline inclusion of all CSS is necessary. This extends beyond font-face fallback rules, automatically handled by Fontaine. Any residual layout shifts during stylesheet loading can be addressed, with ongoing efforts such as this PR aiming to incorporate this capability directly into Nuxt itself.
Whether you opt for pnpm
, npm
, or yarn
, the process of integrating Fontaine into your project is straightforward. Follow the commands below based on your preferred package manager:
pnpm add -D @nuxtjs/fontaine
Adding Fontaine to your Nuxt configuration is a breeze. Simply include it in the modules array, as shown below. Additionally, customization for Google Fonts or fonts without a @font-face declaration is available but often unnecessary.
export default defineNuxtConfig({
modules: ['@nuxtjs/fontaine'],
// Optional: Customize font metrics for Google Fonts or other fonts without @font-face declarations
// fontMetrics: {
// fonts: ['Inter', { family: 'Some Custom Font', src: '/path/to/custom/font.woff2' }],
// },
})
That's it! With minimal configuration, Fontaine seamlessly integrates into your project.
If you're utilizing Tailwind CSS and specifying a custom font in your configuration, manually adding the fallback font is necessary. The example below demonstrates this process:
import type { Config } from 'tailwindcss'
import { fontFamily } from 'tailwindcss/defaultTheme'
export default <Partial<Config>> {
theme: {
extend: {
fontFamily: {
sans: ['Roboto', 'Roboto fallback', ...fontFamily.sans],
},
},
},
}
Fontaine operates by scanning your @font-face rules, generating fallback rules with accurate metrics. The example below illustrates this process:
@font-face {
font-family: 'Roboto';
font-display: swap;
src: url('/fonts/Roboto.woff2') format('woff2'), url('/fonts/Roboto.woff') format('woff');
font-weight: 700;
}
/* Automatically generated fallback */
@font-face {
font-family: 'Roboto fallback';
src: local('BlinkMacSystemFont'), local('Segoe UI'), local('Roboto'), local('Helvetica Neue'),
local('Arial'), local('Noto Sans');
ascent-override: 92.7734375%;
descent-override: 24.4140625%;
line-gap-override: 0%;
}
Whenever you use font-family: 'Roboto', Nuxt seamlessly appends the fallback to the font-family property:
:root {
font-family: 'Roboto';
/* Enhanced to include fallback */
font-family: 'Roboto', 'Roboto fallback';
}
The core functionality of this module isn't confined to Nuxt alone. If you wish to leverage these benefits outside the Nuxt ecosystem, a standalone library, fontaine, is available. Explore its capabilities beyond the Nuxt framework!
As we delve into the intricacies of font metrics and overrides, Fontaine emerges as a valuable asset for enhancing web performance. Its ability to minimize CLS, automate fallbacks, and maintain a lightweight footprint makes it a noteworthy addition to your toolkit. Whether within Nuxt or beyond, consider integrating Fontaine to unlock a smoother, more optimized user experience.