Nuxt Fonts

repository·main·Indexed 20 days ago

https://github.com/nuxt/fonts

A plug-and-play module for Nuxt applications providing automatic web font optimization and configuration. It handles downloading, caching, and optimizing font metrics to improve performance and prevent layout shifts. Supports multiple providers including Google Fonts, Adobe Fonts, Bunny, Fontshare, Fontsource, npm packages, and local files.

Tokens
8.3K
Snippets
42
Records
49
Agent score
69%

What's inside @nuxt/fonts

  1. Overview of Nuxt Fonts features

    main

    Nuxt Fonts is a module designed for optimized custom web font management in Nuxt applications. Key capabilities include:

    • Zero Configuration: Manage font optimization with a single font-family: declaration.
    • Multiple Providers: Support for over 6 built-in font providers to avoid vendor lock-in, with the ability to create custom providers.
    • Local Download Support: Font files are automatically downloaded for production usage, ensuring no external requests are made to font providers at runtime.
    • Font Metric Optimization: Built-in optimization using fontaine and capsize to prevent layout shifts.
    • Automatic Caching: Build and development time font caching powered by unstorage.
  2. How Nuxt Fonts works

    main

    Nuxt Fonts automatically optimizes font loading by processing your CSS and performing the following steps when it encounters a font-family declaration:

    1. Resolves fonts: It searches your public/ directory for matching files (e.g., Roboto.woff2). If not found, it queries configured web font providers like google, bunny, or fontshare.
    2. Injects @font-face rules: It generates and injects @font-face declarations directly into your CSS files to point to the correct source files.
    3. Proxies and caches: Instead of linking directly to remote servers, it uses the /_fonts subpath to proxy requests. This allows the module to download and cache fonts locally.
    4. Creates font fallback metrics: To reduce Cumulative Layout Shift (CLS), Nuxt Fonts generates fallback @font-face rules that 'morph' local system fonts (like Arial or Times New Roman) to match the metrics (ascent, descent, etc.) of the target web font.
    5. Build integration: During the build process, all used fonts are downloaded, hashed, and copied into the project so they can be served with long-lived cache headers without external requests.
  3. How the local font provider works

    main

    The local provider scans your public/ directories (including those in Nuxt layers) for font files with extensions like ttf, woff, woff2, eot, or otf.

    When you use a font-family in CSS, the provider matches it against these files. By default, it expects the 400 weight, normal style, and latin subset. To support different variations, include the metadata in the filename:

    • Default: comic-sans-ms.woff2 (400/normal/latin)
    • Custom: comic-sans-ms-700-italic-cyrillic.woff2 (700/italic/cyrillic)
    • Keywords: You can use light, bold, or black instead of numbers (e.g., comic-sans-ms-bold.woff2).

    Note: The local provider only attempts to load styles and weights that were explicitly configured in your Font Options.

  4. Use the npm provider for self-hosted fonts

    main

    The npm provider resolves fonts from packages installed in your node_modules. It automatically detects known font packages like @fontsource/*, @fontsource-variable/*, and cal-sans from your package.json. By default, fonts are resolved locally from disk (remote: false), meaning no CDN requests are made, making it ideal for self-hosting via npm packages.

    To use it, specify the npm provider in your nuxt.config.ts for specific font families.

    export default defineNuxtConfig({
      modules: ['@nuxt/fonts'],
      fonts: {
        families: [
          // Automatically resolved if @fontsource/roboto is in your package.json
          { name: 'Roboto', provider: 'npm' },
          // Variable fonts are also supported
          { name: 'Inter Variable', provider: 'npm' },
        ],
      },
    })
  5. Use Nuxt Fonts with Tailwind CSS v4

    main

    Tailwind CSS v4 uses CSS variables for configuration. To use a Nuxt Font, define it within an @theme block in your CSS file using a CSS variable.

    Note: If you were previously using processCSSVariables: true for Tailwind v4 support, this option is no longer required or recommended for Nuxt Fonts v0.11.0 and above.

    @theme {
      --font-display: "Inter", "sans-serif";
    }
  6. Use Nuxt Fonts with Pure CSS

    main

    To use Nuxt Fonts in standard CSS, declare the font name within a font-family property.

    Important details:

    • Nuxt Fonts only generates CSS for the first font listed in your font-family declaration. Any subsequent fonts in the list are used solely to generate metrics for font fallbacks.
    • Limitation: Inline styles using font-family within Vue <template> blocks are not currently supported. You must use external or project-level CSS stylesheets.
    div {
      font-family: 'Inter', sans-serif;
    }
  7. Use Nuxt Fonts with UnoCSS

    main

    To use Nuxt Fonts with UnoCSS, add the font to the theme.fontFamily section of your uno.config.js.

    Wind4 (Tailwind4) Preset: If you are using the Wind4 preset, use the font key instead of fontFamily.

    import { defineConfig } from 'unocss'
    
    export default defineConfig({
      theme: {
        fontFamily: {
          inter: 'Inter',
        },
      },
    })
  8. Install @nuxt/fonts in your Nuxt project

    main

    To add Nuxt Fonts to your project, use the Nuxt module installation command. This will add @nuxt/fonts as a dependency and register it in your Nuxt configuration.

    npx nuxt module add fonts
  9. Upgrade to @nuxt/fonts v0.14

    main
    When upgrading to v0.14, be aware that the default font format has changed to woff2 only. This reduces CSS size by providing fewer @font-face src entries, which is sufficient for most modern browsers. If you must support legacy browsers, you need to explicitly configure additional formats in your nuxt.config.ts using the fonts.defaults.formats option.