# Subsetting my Astro fonts at build time cut them by 40%

> astro-glyphcut subsets Fontsource or local fonts in the Astro build. On my site it cut 137 KB of woff2 to 82 KB and found 3 glyphs missing from Fontsource.

By Emil Celustka, 2026-11-18. Web version: https://celustka.dev/blog/astro-font-subsetting-build-time

The three Fontsource Latin files behind this site's fonts weigh 137 KB. Cut down to ASCII plus the punctuation my copy uses, they weigh 82 KB, 40% less. I moved that cut into the Astro build with [astro-glyphcut](https://github.com/ecelustka/astro-glyphcut), a font provider I published, so the subsets can't drift from the font packages.

## The script I ran by hand

I think font subsetting is super important for site performance, so I did it early, with a script around [subset-font](https://github.com/papandreou/subset-font). It read the variable woff2 files from `@fontsource-variable/*`, kept only the glyphs and OpenType features the site uses, and clamped the weight axis:

```ts title="scripts/subset-fonts.ts"
const subset = await subsetFont(source, ascii + extra, {
  targetFormat: "woff2",
  keepFeatures: features,
  variationAxes: { wght: weight },
})
```

I ran it with `pnpm fonts` and committed the output to `src/assets/fonts`. That works until Renovate bumps a font package. The committed files then come from the old version and nothing tells you. I wanted the subset to come out of the build, with no script to remember.

## Moving it into the build

Astro's Fonts API takes [custom font providers](https://docs.astro.build/en/reference/font-provider-reference/#building-a-font-provider). A provider needs a `name` and a `resolveFont()` that returns the font faces for a family, and that's where the subsetting goes. I also wanted to try maintaining my first package and share it with other Astro sites. So astro-glyphcut became a provider, and the site config changed like this (one family shown):

```diff title="astro.config.ts"
+// ASCII plus the punctuation the copy uses.
+const glyphs = glyphcut({
+  text: {
+    ascii: true,
+    extra: " ©«°±·»×‐–—‘’‚“”„•…′″‹›←↑→↓−",
+  },
+})
+
   fonts: [
     {
-      provider: fontProviders.local(),
+      provider: glyphs,
       name: "JetBrains Mono",
       cssVariable: "--font-jetbrains-mono",
+      weights: ["400 700"],
+      styles: ["normal"],
       options: {
-        variants: [
-          {
-            weight: "400 700",
-            style: "normal",
-            src: ["./src/assets/fonts/jetbrains-mono-normal.woff2"],
-          },
-        ],
+        fontsource: "jetbrains-mono",
+        features: ["calt", "ccmp", "locl", "mark"],
       },
```

The provider reads the files from `node_modules`, subsets them with the same subset-font options (here's [the call](https://github.com/ecelustka/astro-glyphcut/blob/v0.1.0/src/core/subset.ts#L36-L40) in 0.1.0), and caches the result in `node_modules/.astro/glyphcut/` until the font, the options or the engine versions change. A feature name the font doesn't have fails the build, so a typo in `features` stops the build instead of dropping kerning.

The output matched the committed files byte for byte, so I deleted the script and `src/assets/fonts`. Sizes in bytes, with astro-glyphcut 0.1.0 on Astro 7.4.12:

| Font | Fontsource | Google | glyphcut |
| --- | ---: | ---: | ---: |
| JetBrains Mono | 40,404 | 27,232 | 23,852 |
| Schibsted Grotesk | 46,752 | 38,464 | 28,008 |
| Schibsted Grotesk italic | 50,300 | 41,168 | 30,496 |
| Total | 137,456 | 106,864 | 82,356 |

Fontsource is the Latin file from the package, and glyphcut saves 39% to 41% on each. The Google column is Astro's Google provider with its [`experimental.glyphs`](https://docs.astro.build/en/reference/font-provider-reference/#experimentalglyphs) option, which only works for Google fonts. I gave it the same glyphs, weights and styles, and its files came out 30% bigger than astro-glyphcut's. They keep more of the weight axis (400 to 900 on Schibsted Grotesk, where the site uses 400 to 800) and features the site doesn't ask for, like `frac` and `pnum`.

## Three characters Fontsource left out

When I built the site to measure for this post, the build printed warnings I hadn't seen before:

```txt title="astro build"
[glyphcut] jetbrains-mono-normal: 40 404 → 23 852 B
[glyphcut] jetbrains-mono: no glyph for "‐" (U+2010)
[glyphcut] jetbrains-mono: no glyph for "←" (U+2190)
[glyphcut] jetbrains-mono: no glyph for "→" (U+2192)
[glyphcut] schibsted-grotesk-normal: 46 752 → 28 008 B
[glyphcut] schibsted-grotesk: no glyph for "‐" (U+2010)
…
```

My `extra` string has the Unicode hyphen (not the ASCII `-`) and both arrows, but the Fontsource Latin files don't, so the browser has drawn every one of them in the fallback font. The fonts themselves have both arrows, and JetBrains Mono has the hyphen too, since the Google build above kept them. None of the Fontsource subset files for these two fonts has any of the three. The old script never said so, because it only logged the file sizes. astro-glyphcut compares the glyph set with the font's own character map and [logs each miss](https://github.com/ecelustka/astro-glyphcut/blob/v0.1.0/src/provider.ts#L131). It only warns, since the fallback font still renders the character. After these warnings I took the three characters out of `extra` and wrote the post navigation arrows as `<-` and `->`, which JetBrains Mono draws as ligatures.

## Check your own fonts

Build the site and list the font files:

```sh
$ find dist -name '*.woff2' -exec wc -c {} +
```

```txt title="stdout"
   30496 dist/_astro/fonts/01ab208c97eea403.woff2
   28008 dist/_astro/fonts/530d6a0ae19e2507.woff2
   23852 dist/_astro/fonts/8077b4b949ecced1.woff2
   82356 total
```

Compare the sizes with the font files in `node_modules/@fontsource*/<font>/files`. If they match, your visitors download glyphs your site never shows. Subset them at build time. On my fonts that was an easy 40%.
