To preload a font in Astro, add the preload prop to the <Font> component from astro:assets: <Font cssVariable="--font-inter" preload />. Astro then outputs a <link rel="preload" as="font" crossorigin> for each file of that family. Pass an array such as preload={[{ weight: 400 }]} to preload only the files the first screen needs.
Key Takeaways
preloaddefaults tofalse; withtrueevery file of the family is preloaded.- A filter array matches on
weight,styleandsubset, and weight ranges match variable files.- Preload one or two files at most; every preload competes with your CSS and LCP image.
- Without the Fonts API, import the file with
?urland write the link tag yourself.
With the Fonts API
// astro.config.mjs
import { defineConfig, fontProviders } from 'astro/config';
export default defineConfig({
fonts: [
{
provider: fontProviders.google(),
name: 'Inter',
cssVariable: '--font-inter',
weights: [400, 700],
styles: ['normal', 'italic'],
},
],
});
---
// src/layouts/Layout.astro
import { Font } from 'astro:assets';
---
<head>
<Font cssVariable="--font-inter" preload={[{ weight: 400, style: 'normal' }]} />
</head>
That config produces four files (two weights, two styles), but only regular 400 is preloaded. Bold and italic still load normally when the page uses them. With a variable font configured as weights: ['100 900'], preload={[{ weight: 400 }]} matches the single variable file because 400 falls inside its range.
Filters can also target subsets, for example preload={[{ subset: 'latin' }]} when you also load latin-ext but most visitors only need Latin. Our Google Fonts tip covers the config options.
Without the Fonts API
For a font file you manage yourself, declare it in your global CSS with a relative path. Vite resolves the path, hashes the file and rewrites the URL:
/* src/styles/global.css */
@font-face {
font-family: 'Inter';
src: url('../assets/fonts/Inter-Variable.woff2') format('woff2');
font-weight: 100 900;
font-display: swap;
}
Then import the same file with ?url in the layout to get the identical hashed URL for the preload tag:
---
// src/layouts/Layout.astro
import '../styles/global.css';
import interUrl from '../assets/fonts/Inter-Variable.woff2?url';
---
<head>
<link rel="preload" href={interUrl} as="font" type="font/woff2" crossorigin />
</head>
A simpler alternative is to keep the file in public/fonts/ and use /fonts/Inter-Variable.woff2 in both the link and your CSS. You lose the content hash, so set a sensible cache policy on your host. crossorigin is required even for same-origin fonts; without it the browser downloads the file twice.
When not to preload
- Fonts used only below the fold. Let them load on demand.
- Many weights. Preloading five files delays the CSS and hero image that actually set LCP.
- Icon fonts. Prefer SVG icons, which need no font at all.
Check the effect in Lighthouse or WebPageTest: preloading should move the font request earlier without pushing back the LCP image. The Fonts API also generates metric-adjusted fallbacks, which reduce the layout shift when the font swaps in. Our Fonts API deep dive explains those fallbacks, and the official fonts guide documents the preload prop. To change the font in one of our Astro themes, see the theme styling docs.
FAQ
Should I preload fonts in Astro?
Preload the one font file used for body text or headings on the first screen. Preloading more files often slows the page down, because they compete with CSS and the main image.
How do I preload only one weight with the Astro Font component?
Pass a filter array: <Font cssVariable="--font-inter" preload={[{ weight: 400 }]} />. You can also filter by style and subset.
Why does my preloaded font download twice?
The preload link is missing crossorigin. Fonts are always fetched in CORS mode, so a preload without it cannot be reused and the browser fetches the file again.