Web fonts are one of the most common causes of layout shift on Gatsby sites. The usual setup is a <link> to Google Fonts in gatsby-ssr.js or a Head export. That adds a third-party connection, blocks rendering on a stylesheet, and still produces a visible jump when the real font swaps in. A Gatsby build gives you everything you need to fix this, because the HTML is static and you control every asset.
This tutorial walks through a setup that works on Gatsby 5: self-hosted fonts, one preload, and a fallback font tuned to match metrics.
1. Install fonts as packages
Fontsource ships font files as npm packages, so versions are pinned and files are served from your own domain.
npm install @fontsource-variable/inter
Import it once in gatsby-browser.js and gatsby-ssr.js, or in a layout that always renders:
// gatsby-browser.js
import "@fontsource-variable/inter"
A variable font covers all weights in one file. That usually beats loading four static weights. Subset to Latin if your site is English-only by importing the subset CSS, for example @fontsource-variable/inter/wght.css, and check which unicode-range blocks the package defines.
2. Preload only the file that matters
Preloading every font file wastes bandwidth. Preload just the Latin file used above the fold. Gatsby hashes imported assets, so import the file URL and use it in the Head API.
import interLatin from "@fontsource-variable/inter/files/inter-latin-wght-normal.woff2"
export const Head = () => (
<link rel="preload" as="font" type="font/woff2"
href={interLatin} crossOrigin="anonymous" />
)
If your bundler config does not import .woff2 as a URL, copy the file into static/fonts/ and reference /fonts/inter-latin-wght-normal.woff2 instead. The crossOrigin attribute is required even for same-origin fonts, otherwise the browser downloads the file twice.
3. Set font-display deliberately
Fontsource CSS uses font-display: swap. That is fine for body text as long as the fallback matches the real font closely, which is the next step. For decorative display fonts, optional avoids the swap altogether on slow connections.
4. Add a metric-matched fallback
Layout shift happens because the fallback font has different widths and line heights. A @font-face declaration with size-adjust, ascent-override, descent-override and line-gap-override makes a local font behave like the web font.
@font-face {
font-family: "Inter Fallback";
src: local("Arial");
size-adjust: 107%;
ascent-override: 90%;
descent-override: 22%;
line-gap-override: 0%;
}
body {
font-family: "Inter Variable", "Inter Fallback", sans-serif;
}
The numbers above are illustrative. Compute the real ones for your font with a tool such as Capsize or the fontaine library, then paste the output into your global CSS.
5. Cache the files for a year
Hashed font files never change, so serve them with long-lived headers. On Gatsby, hashed assets live under /static/. Add a header rule at your host:
/static/*
Cache-Control: public, max-age=31536000, immutable
If you copied fonts to static/fonts/ without hashes, use a shorter max-age, or version the filename.
6. Verify
- Run
gatsby build && gatsby serve, then load a page with the network panel throttled to Slow 4G. - In DevTools, open Performance, record a load, and check the Layout Shifts track. Font swap should no longer produce a shift.
- Confirm there are no requests to
fonts.googleapis.comorfonts.gstatic.com. - Compare CLS in Lighthouse before and after, and then watch field data in your RUM tool for a week, since lab numbers hide real-world variation.
Common mistakes
- Loading both Google Fonts and Fontsource during a migration, so the font downloads twice.
- Preloading several weights and italics that are not used on first paint.
- Forgetting
crossOriginon the preload. - Using a fallback with no metric overrides and expecting
swapto be invisible.
Self-hosting fonts is a small change, typically under an hour on an existing Gatsby site, and it removes a third-party dependency along with a layout shift. If you have a Gatsby site with stubborn CLS or performance regressions, our team can audit it and help.