Skip to content

Font optimization

A web font is discovered late: the browser finds it only after the CSS that names it has loaded. Font optimization self-hosts the files, generates the @font-face rules, and can preload the file. In principle, self-hosting the font (via either route) removes a cross-origin request from the critical path.

02 / 14First-paint leverOne of 7 features that affect first paint, the moment the browser first draws content. Together they are worth 14 of the 28 points.

Labels across 23 frameworks

  • 1st-party4
  • Experimental0
  • Third-party19
  • Manual0
  • of 23

How to enable it, by framework

One card per framework, in the same order on every feature page. Where a framework has no built-in support (Manual), a short example shows how to build it yourself.

Next

1st-party2 ptsOverview

next/font/google self-hosts, subsets, generates the @font-face, and preloads at build time. No config beyond using it in the layout.

import { Geist } from 'next/font/google';

const geistSans = Geist({
  display: 'block',
  subsets: ['latin'],
  variable: '--font-geist-sans',
});

From this repo's next-books/app/layout.tsx (the Geist_Mono import and instance omitted).

Nuxt

1st-party2 ptsOverview

Add the official @nuxt/fonts module. It resolves the fonts used in CSS, downloads them at build time, serves them from your own origin, injects @font-face rules, generates fallback metrics for lower CLS, and can subset.

export default defineNuxtConfig({
  modules: [
    '@nuxt/fonts',
  ]
})

Adapted from the module page. This port does not use it; it imports @fontsource-variable/geist manually, so the capability exists in the framework even though this repo took the third-party route:

@import '@fontsource-variable/geist';

From this repo's nuxt-books/app/assets/css/main.css (optimized branch).

SvelteKit

Third-party1 ptOverview

Import @fontsource-variable/geist (Fontsource). SvelteKit has no first-party font module and does not preload fonts by default; the handle hook's resolve(..., { preload }) filter can add font preloads (not used here), so the font is discovered from the inlined stylesheet.

@import "@fontsource-variable/geist";

From this repo's svelte-books/src/routes/layout.css (optimized branch).

SolidStart

Third-party1 ptOverview

Use @fontsource-variable/geist plus an explicit <link rel="preload" as="font"> with the hashed woff2 URL (static assets; no first-party font primitive documented — that page covers /public and asset imports only).

import geistLatin from "@fontsource-variable/geist/files/geist-latin-wght-normal.woff2?url";
// ...
<link rel="preload" as="font" type="font/woff2" href={geistLatin} crossorigin="" />

From this repo's solid-books/src/Document.tsx (optimized branch).

Import @fontsource in your global CSS; there is no preload, so the font is discovered from the inlined CSS. No first-party font module is documented; the production checklist only says to check font loading.

@import '@fontsource-variable/geist';

From this repo's tanstack-books/src/styles/global.css (optimized branch).

Hand-write @font-face rules over the Fontsource woff2 files in src/app.css, plus an explicit woff2 preload in __root.tsx. No first-party font module is documented (same production checklist).

@font-face {
  font-family: "Geist Variable";
  font-display: optional;
  font-weight: 100 900;
  src: url("@fontsource-variable/geist/files/geist-latin-wght-normal.woff2") format("woff2");
}
links: [
  { rel: "preload", as: "font", type: "font/woff2", href: geistLatin, crossOrigin: "anonymous" },
],

From this repo's tanstack-solid-books/src/app.css and src/routes/__root.tsx (optimized branch; font-style and unicode-range omitted).

Pracht

1st-party2 ptsOverview

defineFont() from @pracht/core generates @font-face rules and preload links by default; fallback-metric overrides (sizeAdjust, ascentOverride, ...) are opt-in with hand-supplied values, and there is no build-time download or subsetting, so it is not equivalent to next/font.

export const interRegular = defineFont({ family: "Inter", src: "/fonts/inter-400.woff2", weight: 400 });
// head: { fonts: [interRegular, interBold] }

Adapted from the fonts docs. This port chose @fontsource-variable/geist in src/styles/global.css instead.

Waku

Third-party1 ptOverview

Waku's docs recommend @fontsource/* because there is no next/font equivalent (migration guide). This is the optimized branch; main still loads Geist from fonts.googleapis.com with display=block.

@import '@fontsource-variable/geist';

From this repo's waku-books/src/styles.css (optimized branch).

Ember

Third-party1 ptOverview

Import the font from Fontsource on optimized; main loads Geist from fonts.googleapis.com with preconnect and display=swap. No first-party font module is documented (the Vite guide covers build setup only).

@import "@fontsource-variable/geist";

From this repo's ember-books/app/styles/app.css (optimized branch).

Astro

1st-party2 ptsOverview

Fonts API: top-level fonts config (astro@6.0.0+) plus <Font cssVariable preload /> from astro:assets, not under experimental flags. Providers include Google, Bunny, Fontsource, Fontshare, Adobe, local; fonts are downloaded, cached, served from _astro/fonts, with a subsets option (default latin) and optimized fallbacks. preload defaults to false. (fonts, astro-assets)

Code example

Register the font in the top-level fonts config, then render <Font /> in the layout head. Astro downloads and serves the files itself, and preload adds a preload link.

// astro.config.mjs
import { defineConfig, fontProviders } from 'astro/config';

export default defineConfig({
  fonts: [{
    provider: fontProviders.fontsource(),
    name: 'Roboto',
    cssVariable: '--font-roboto',
  }],
});
---
// src/layouts/Layout.astro
import { Font } from 'astro:assets';
---
<html>
  <head>
    <Font cssVariable="--font-roboto" preload />
  </head>
  <body><slot /></body>
</html>

Source: Astro: Using custom fonts

React Router

Third-party1 ptOverview

No first-party font primitive documented; the styling page does not cover fonts (use Fontsource or a CDN by hand). (styling)

Code example

The port installs Fontsource's variable Geist packages and imports them at the top of the global stylesheet. The packages ship the @font-face rules and self-hosted font files, and Vite bundles them like any other CSS import.

/* app/styles/global.css */
@import '@fontsource-variable/geist';
@import '@fontsource-variable/geist-mono';
@import 'tailwindcss';

@theme {
  /* … */
  --font-geist-sans: 'Geist Variable', 'Geist', ui-sans-serif, system-ui, sans-serif;
  --font-geist-mono: 'Geist Mono Variable', 'Geist Mono', ui-monospace, monospace;
  --font-sans: var(--font-geist-sans);
  --font-mono: var(--font-geist-mono);
}

Source: Fontsource: Installing fonts

Qwik City

Third-party1 ptOverview

The cookbook recommends Fontsource, manual @font-face, unicode-range, and Fontaine for fallback metrics; no preload guidance and no first-party font module documented. (fonts)

Code example

The Qwik cookbook points to Fontsource for self-hosted files and Fontaine for fallback metrics. Install @fontsource-variable/open-sans and fontaine, import the font in the root layout, and add the Fontaine Vite plugin.

// src/routes/layout.tsx
import '@fontsource-variable/open-sans/wght.css'
// global.css: body { font-family: "Open Sans Variable", sans-serif; }
// vite.config.ts
import { FontaineTransform } from 'fontaine'
// …
plugins: [
  qwikCity(), qwikVite(),
  FontaineTransform.vite({
    fallbacks: ['Arial'],
    resolvePath: (id) => new URL(`./public${id}`, import.meta.url),
  }),
],

Source: Qwik: Fonts cookbook, Fontsource: Qwik guide

Angular (SSR)

Third-party1 ptOverview

optimization.fonts.inline (default true) inlines external Google Fonts and Adobe Fonts CSS into the index HTML (needs internet at build). That removes one render-blocking stylesheet request, but the font files still load from the font host; no self-hosting, subsetting, or preload is documented. Self-hosting needs a package such as Fontsource, as for React Router, Qwik City, Marko, and Fresh. Our feature test confirmed this: with fonts.inline on, the Google CSS was inlined and the font file was still fetched from fonts.gstatic.com. (workspace-config, Fontsource)

Code example

Link a Google Fonts stylesheet in index.html as usual. With optimization.fonts.inline on, the production build downloads that CSS and inlines it into the HTML, removing one render-blocking request.

<!-- src/index.html -->
<head>
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet" />
</head>
// angular.json (production configuration; needs internet access at build time)
"optimization": {
  "fonts": { "inline": true }
}

Source: Angular: Workspace configuration

Marko

Third-party1 ptOverview

No font primitive documented; self-host by hand with a community package such as Fontsource (as the marko-books optimized branch does). The Core Tags reference only shows a Google Fonts @import inside <html-style> (a raw <style> tag). No first-party font handling documented. (core-tag, styling)

Code example

The Books port self-hosts Geist with Fontsource. The CSS @import pulls the package's @font-face rules, and Vite copies the .woff2 files into the build with hashed names.

/* src/styles/global.css */
@import '@fontsource-variable/geist';
@import '@fontsource-variable/geist-mono';
@import 'tailwindcss';

@theme {
  --font-sans: 'Geist Variable', ui-sans-serif, system-ui, sans-serif;
  --font-mono: 'Geist Mono Variable', ui-monospace, monospace;
}

Source: Fontsource: Install

Fresh

Third-party1 ptOverview

No first-party font handling documented; fonts appear only as files in static/ referenced by URL. The third-party route is Fontsource packages, which the Fresh docs do not mention. (static-files)

Code example

The Books port self-hosts Geist with Fontsource packages mapped in deno.json. The CSS @import brings in the @font-face rules, and Vite bundles the font files when client.ts imports the stylesheet.

// deno.json
"imports": {
  "@fontsource-variable/geist": "npm:@fontsource-variable/geist@^5.3.0"
}
/* assets/global.css (imported from client.ts) */
@import '@fontsource-variable/geist';
@import 'tailwindcss';

@theme {
  --font-sans: 'Geist Variable', ui-sans-serif, system-ui, sans-serif;
}

Source: Fontsource: Install

Analog

Third-party1 ptOverview

No first-party font handling documented; the style pipeline guide covers stylesheet preprocessing only. The third-party route is Fontsource packages imported from the global stylesheet. (style-pipeline)

Code example

Install a Fontsource package and import it from the global stylesheet. Vite bundles the font files with hashed names and serves them from your own origin.

/* src/styles.css (npm install @fontsource-variable/open-sans) */
@import '@fontsource-variable/open-sans';

body {
  font-family: 'Open Sans Variable', sans-serif;
}

Source: Fontsource: Install

Vike

Third-party1 ptOverview

No first-party font module documented. Vike injects <link rel="preload" as="font"> for font files a page imports, but it does not download, subset or self-host fonts; the third-party route is Fontsource. (preloading)

Code example

Install a Fontsource package and import it from +Layout. The font files are then local build assets, and Vike adds a <link rel="preload" as="font"> for them.

// pages/+Layout.tsx
import '@fontsource-variable/inter'
import './Layout.css' // body { font-family: "Inter Variable", sans-serif; }

export function Layout({ children }: { children: React.ReactNode }) {
  return <div id="app">{children}</div>
}

Source: Fontsource: Install · Vike: Preloading

RedwoodSDK

Third-party1 ptOverview

No first-party font handling documented; the docs show font files in public/fonts with a hand-written @font-face. The third-party route is Fontsource. (public-assets)

Code example

Install a Fontsource package and import it at the top of the stylesheet. The package ships the @font-face rules and self-hosted font files, and Vite bundles them like any other CSS import.

/* src/app/styles.css */
@import "@fontsource-variable/inter";
@import "tailwindcss";

@theme {
  --font-sans: "Inter Variable", ui-sans-serif, system-ui, sans-serif;
}

Source: Fontsource: Installing fonts

Remix 3

Third-party1 ptOverview

No first-party font module documented. The asset server rewrites url() references in CSS, and a font preload is a hand-written <link rel="preload" as="font"> using assets.getHref(); the third-party route is Fontsource. (files-and-assets)

Code example

Install Fontsource, allow the package in the asset server, and link its stylesheet. The asset server rewrites the url() references in the CSS. The preload is a hand-written <link> with a URL from getHref().

// app/assets.ts  (npm i @fontsource-variable/inter)
export const assets = createAssetServer({
  // …
  allowPackages: ["remix", "@fontsource-variable/inter"],
  files: { extensions: [".svg", ".png", ".jpg", ".woff2"] },
});

export const fontCssHref = await assets.getHref(
  "node_modules/@fontsource-variable/inter/index.css",
);
export const fontHref = await assets.getHref(
  "node_modules/@fontsource-variable/inter/files/inter-latin-wght-normal.woff2",
);

// app/actions/document.tsx, inside <head>
<link rel="preload" href={fontHref} as="font" type="font/woff2" crossOrigin="anonymous" />
<link rel="stylesheet" href={fontCssHref} />

Source: Remix: Files and assets, reference styles, images, and fonts

Void (React)

Third-party1 ptOverview

No first-party font handling documented; the docs mention fonts only as CSS variables for the Markdown theme. The third-party route is Fontsource packages imported from a stylesheet. (markdown, static-assets)

Code example

Install a Fontsource package so the font files become local build assets, then add the preload link from the page's head.

// pages/index.server.ts
import '@fontsource-variable/geist' // @font-face with font-display: swap
import fontUrl from '@fontsource-variable/geist/files/geist-latin-wght-normal.woff2?url'
import { defineHead } from 'void'

export const head = defineHead(() => ({
  link: [{ rel: 'preload', href: fontUrl, as: 'font', type: 'font/woff2', crossorigin: 'anonymous' }],
}))

Source: Fontsource: Install · Void: Head

Void (Vue)

Third-party1 ptOverview

No first-party font handling documented; the docs mention fonts only as CSS variables for the Markdown theme. The third-party route is Fontsource packages imported from a stylesheet. (markdown, static-assets)

Code example

Install a Fontsource package so the font files become local build assets, then add the preload link from the page's head.

// pages/index.server.ts
import '@fontsource-variable/geist' // @font-face with font-display: swap
import fontUrl from '@fontsource-variable/geist/files/geist-latin-wght-normal.woff2?url'
import { defineHead } from 'void'

export const head = defineHead(() => ({
  link: [{ rel: 'preload', href: fontUrl, as: 'font', type: 'font/woff2', crossorigin: 'anonymous' }],
}))

Source: Fontsource: Install · Void: Head

Void (Svelte)

Third-party1 ptOverview

No first-party font handling documented; the docs mention fonts only as CSS variables for the Markdown theme. The third-party route is Fontsource packages imported from a stylesheet. (markdown, static-assets)

Code example

Install a Fontsource package so the font files become local build assets, then add the preload link from the page's head.

// pages/index.server.ts
import '@fontsource-variable/geist' // @font-face with font-display: swap
import fontUrl from '@fontsource-variable/geist/files/geist-latin-wght-normal.woff2?url'
import { defineHead } from 'void'

export const head = defineHead(() => ({
  link: [{ rel: 'preload', href: fontUrl, as: 'font', type: 'font/woff2', crossorigin: 'anonymous' }],
}))

Source: Fontsource: Install · Void: Head

Void (Solid)

Third-party1 ptOverview

No first-party font handling documented; the docs mention fonts only as CSS variables for the Markdown theme. The third-party route is Fontsource packages imported from a stylesheet. (markdown, static-assets)

Code example

Install a Fontsource package so the font files become local build assets, then add the preload link from the page's head.

// pages/index.server.ts
import '@fontsource-variable/geist' // @font-face with font-display: swap
import fontUrl from '@fontsource-variable/geist/files/geist-latin-wght-normal.woff2?url'
import { defineHead } from 'void'

export const head = defineHead(() => ({
  link: [{ rel: 'preload', href: fontUrl, as: 'font', type: 'font/woff2', crossorigin: 'anonymous' }],
}))

Source: Fontsource: Install · Void: Head

Gotchas

  • Importing Fontsource alone does not preload anything. Without a <link rel="preload"> the browser finds the font only after the stylesheet is parsed.