DEV Community

Daniel Pertu
Daniel Pertu

Posted on

Eleven font weights, 37 font-face rules and ten files on every page of our site, and the three preload tags are missing from the homepage

Munchable's site is three Google fonts deep: Nunito for body text, Outfit for headings, Fredoka for the brand. Self-hosted through next/font/google, which downloads the files at build time so a page view never hands the visitor's IP to Google.

The declaration is twenty lines and completely unremarkable:

const nunito = Nunito({ subsets: ['latin'], weight: ['400', '500', '600', '700'], variable: '--font-nunito', display: 'swap' });
const outfit = Outfit({ subsets: ['latin'], weight: ['500', '600', '700', '800'], variable: '--font-outfit', display: 'swap' });
const fredoka = Fredoka({ subsets: ['latin'], weight: ['500', '600', '700'], variable: '--font-fredoka', display: 'swap' });
Enter fullscreen mode Exit fullscreen mode

Eleven weights. I went looking for what eleven weights actually cost, expecting to find eleven files and a lecture to write about trimming them. What I found was more interesting in both directions.

40 rules, 37 files, ten files

The stylesheet the site serves contains 40 @font-face rules. Three of them have no font file at all:

@font-face{font-family:Nunito Fallback;src:local(Arial);ascent-override:99.71%;descent-override:34.82%;line-gap-override:0.0%;size-adjust:101.39%}
@font-face{font-family:Outfit Fallback;src:local(Arial);ascent-override:100.18%;descent-override:26.05%;line-gap-override:0.0%;size-adjust:99.82%}
@font-face{font-family:Fredoka Fallback;src:local(Arial);ascent-override:95.64%;descent-override:23.17%;line-gap-override:0.0%;size-adjust:101.84%}
Enter fullscreen mode Exit fullscreen mode

Those are Next measuring each real typeface against Arial and writing the overrides that make local Arial occupy the same space, so the swap from fallback to brand font does not move the text. Remember these three, they matter at the end.

The other 37 point at files, and the thing I had wrong is that there are not 37 files. There are ten. Here is the same file, four times:

Nunito 400  224e262ef877bfa4-s...woff2
Nunito 500  224e262ef877bfa4-s...woff2
Nunito 600  224e262ef877bfa4-s...woff2
Nunito 700  224e262ef877bfa4-s...woff2
Enter fullscreen mode Exit fullscreen mode

One file, four weight descriptors. These are variable fonts, so the file carries a weight axis and Next declares a discrete face per weight you asked for, all pointing at the same bytes. Which means the thing everyone's performance intuition says ("four weights is four downloads") is wrong here, and asking for a fifth weight would have cost zero additional bytes.

Where the ten comes from is the other axis: character ranges.

Nunito   | latin               | preloaded | 39,152 bytes
Nunito   | latin-ext          |           | 35,464
Nunito   | vietnamese         |           | 13,040
Nunito   | cyrillic           |           | 20,780
Nunito   | cyrillic-ext       |           | 28,908
Outfit   | latin               | preloaded | 32,228
Outfit   | latin-ext          |           | 14,760
Fredoka  | latin               | preloaded | 29,704
Fredoka  | latin-ext          |           |  4,564
Fredoka  | hebrew             |           |  8,776
Enter fullscreen mode Exit fullscreen mode

227,376 bytes of woff2 in the build, of which the three latin subsets are 101,084. The other seven exist because the families ship those subsets and are declared with a unicode-range, so a browser fetches one only if a character in that range is actually on the page.

So I checked whether any ever are. Across 30 pages, including every list hub and a spread of the 461 answer pages, the only code points above U+00FF in the visible text are U+2019 (a curly apostrophe, 23 times, mostly in "Crohn's") and U+20AC (the euro sign, three times, on the pricing section). Both of those are inside the latin subset's declared range. The site is lang="en-GB" and prices in 24 currencies but writes in one language, so the Vietnamese, Cyrillic and Hebrew faces are 71,504 bytes of correctly-declared, permanently unused src URLs, and the three latin-ext ones another 54,788.

That is not a bug and I am not going to fix it. They cost three declarations each in a stylesheet that is cached forever and zero bytes of transfer. It is just a striking ratio: ten files declared, three ever fetched, and the three are the only ones Next preloads.

The three weights nothing asks for

Having found that weights are free, I went looking for whether we use them. Every font-weight in the public stylesheet, with counts:

font-weight: 700   34
font-weight: 800   11
font-weight: 600   11
Enter fullscreen mode Exit fullscreen mode

That is the complete list. Plus the implicit 400 on body. Nothing in 3,153 lines of CSS, and nothing in any component, asks for weight 500, which we requested from all three families.

On a normal day that is three wasted downloads and a cleanup commit. Here it is three extra lines in a stylesheet and nothing else, because of the shared-file thing above. The reason I am mentioning it anyway is that the audit instinct ("you declared it, you are not using it, delete it") and the actual cost model have come apart, and I would have deleted them on sight a day earlier. With variable fonts, the weight list is not a download list. It is a list of which discrete values the CSS is allowed to name.

There is one place where that bites. Two SVG illustrations on the homepage set their type by hand:

<text fontFamily="Nunito, sans-serif" fontWeight="800" fontSize="9.5" fill="#3b2314">
Enter fullscreen mode Exit fullscreen mode

Nunito is declared at 400, 500, 600 and 700. There is no 800 face for that family, so the browser matches the nearest one available and draws it at 700. Outfit, in the next illustration down, does have an 800 and renders at 800. Two SVGs in the same component, both asking for the same number, getting different answers, because the number is checked against a per-family list neither of them names.

Those illustrations also address the families by their real names rather than through the --font-nunito variable the rest of the site uses, which works only because next/font happens to keep the family name Nunito in its generated @font-face. That is the kind of line that survives until a version bump decides to hash the family name.

The part that actually matters

Now the finding I did not go looking for.

next/font emits preload tags for the files it expects you to need. On an answer page, view source and the head opens with them:

<link rel="preload" href="/_next/static/media/07454f8ad8aaac57-s.p...woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="/_next/static/media/1b99372b3eaef0c8-s.p...woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="/_next/static/media/5d52bd6c4cb3f315-s.p...woff2" as="font" type="font/woff2" crossorigin>
<link rel="stylesheet" href="/_next/static/chunks/02oek0qvthewq.css">
Enter fullscreen mode Exit fullscreen mode

Three preloads, ahead of the stylesheet, exactly where you want them: the browser's preload scanner reads them off the raw bytes of the document before any CSS has been parsed, and the fonts start downloading in parallel with everything else.

I sampled 40 of the 523 URLs in the sitemap at random. All 40 have those three tags. Then I looked at the homepage.

The homepage head has five link tags and not one of them is a font. The preload hints are still there, but they are inside the streamed React payload, as entries in a script near the end of the document:

:HL["/_next/static/media/07454f8ad8aaac57-s.p...woff2","font",{"crossOrigin":"","type":"font/woff2"}]
Enter fullscreen mode Exit fullscreen mode

Those become link elements when React processes them on the client. Which is strictly later than the parser would have found a tag in the head, and the preload scanner, whose entire job is to start fetches before the parser gets that far, never sees them at all.

Two pages on the site behave this way, and they have one thing in common:

/            cache-control: private, no-cache, no-store   (no head preloads)
/support     cache-control: private, no-cache, no-store   (no head preloads)
/answers     x-nextjs-prerender: 1                        (three head preloads)
/about       x-nextjs-prerender: 1                        (three head preloads)
Enter fullscreen mode Exit fullscreen mode

The two are the two that are not prerendered. /support reads a session, and the homepage opts out on purpose:

/**
 * Rendered per request rather than cached.
 *
 * Localising the price means reading the visitor's country from the request,
 * which is incompatible with serving one cached copy of the page to everyone.
 * The alternative, caching GBP and correcting it in the browser, is what
 * produces a visible price flicker, so the cache is what gives way.
 */
export const dynamic = 'force-dynamic';
Enter fullscreen mode Exit fullscreen mode

I am not going to claim I can tell you precisely which code path inside Next decides this; what I can tell you is the observable rule, which is that the font preload tags come with static rendering and the dynamic routes get the flight hints instead. Which is a trade nobody wrote on the tin. I chose force-dynamic to avoid a price flicker and paid for it in font discovery on the one page where first impression is the entire job.

You can check both halves yourself in about thirty seconds. View source on munchable.app and search the head for as="font": nothing. Do the same on munchable.app/answers or /fodmap-food-list: three tags, before the stylesheet.

Why it has not shown up as a complaint

Because of the three rules I asked you to remember. display: 'swap' plus Nunito Fallback with size-adjust: 101.39% means the homepage paints its text immediately in metric-matched Arial, and the swap to Nunito does not reflow anything. So the symptom is not a blank hero or a jumping headline. It is a fraction of a second of slightly-wrong letterforms, which is exactly the sort of defect that never gets reported and never gets fixed.

The honest summary of the whole audit: the thing I expected to find (too many weights) was free, the thing that looked like dead weight (seven unused subsets) costs nothing, the small real bug (an 800 that does not exist) is invisible, and the actual regression came from a caching decision made three files away for reasons that had nothing to do with fonts. Which is roughly how performance work always goes.

Top comments (0)