JD
EN/DE
All writing
5 min read

Why one in fourteen of my rendered posts shipped in the wrong font

A headless renderer that screenshots before the brand font arrives fails silently and only under load. Removing the network from the render, not adding a longer wait, took fallback-font images from 7.1% to zero.

  • Playwright
  • Web fonts
  • AI pipeline
A contact sheet of fourteen rendered social post cards, all reporting success. Thirteen are set in their brand fonts; one, circled in orange, fell back to Times and its headline is cut off at the bottom. Fallback renders went from 7.1% to zero.

My AI Content Studio turns a company URL into a brand kit and a set of ready-to-publish social posts. The last step is unglamorous: each post is an HTML template filled with the model's copy and the brand's colours and fonts, and a headless Chromium instance, driven by Playwright, screenshots it to a PNG. It is the step nobody looks at until a customer sends back an image of their headline set in Times New Roman.

The problem

The first report looked like a one-off. The second came a week later from a different brand. When I pulled every image the pipeline had produced over a fortnight and checked them by hand, 85 of 1,200 renders, about 7.1%, had been captured with a fallback font. Because fallback fonts have different widths, the damage went further than looks: on 41 of them the headline reflowed onto an extra line and was clipped by the bottom of the card.

Nothing had failed. Every job reported success, every PNG had the right dimensions, and the copy was correct. Locally, rendering one post at a time, I could not reproduce it at all. It only appeared in production, and more often on busy days.

Finding the cause

The render code did what most examples do: load the HTML with page.setContent, wait for the page's load event, then call document.fonts.ready before taking the screenshot. That looks safe. It is not, for two reasons that only bite together.

First, the browser does not download a web font when it parses the @font-face rule. It downloads it when layout first needs a glyph in that face. document.fonts.ready resolves when there are no font loads pending, so if you ask before layout has requested the brand font, the honest answer is that nothing is pending, and it resolves straight away. The load event has the same blind spot: it does not wait for fonts that have not been requested yet.

Second, the brand fonts came from wherever the brand kit had found them, usually the customer's own site or CDN. Locally those requests took tens of milliseconds and won the race. In production, with several renders running in parallel against slow or rate-limited origins, they regularly lost it. A few were never going to win: the origin refused cross-origin font requests, the load failed quietly, and the browser fell back without an error anywhere.

To confirm it, I added a small probe that ran in the page immediately before each screenshot and logged, for every face the template used, the result of document.fonts.check. In a replay of 300 renders at production concurrency, every fallback image matched a probe entry showing the brand face as not loaded. Every correct image matched an entry showing it as loaded. That ruled out the CSS and the templates.

The fix

The tempting fix was a longer wait. A fixed timeout would have hidden the problem on most days and made every render slower on all of them, and it would never help the fonts that were failing outright. So I took the network out of the render instead.

1. Fonts are fetched once, at brand-kit time, not at render time. The pipeline downloads each font file, checks it is a real WOFF2, WOFF or TTF file by its signature rather than trusting the URL, and stores it with the brand kit. If a font cannot be fetched, the brand kit says so and offers a close open-licence alternative, so the decision is visible instead of being made silently by the browser.

2. At render time the fonts are inlined into the HTML as base64 data URIs in the @font-face rules. The page now has nothing to fetch, so there is no race left to lose.

3. Before the screenshot, the renderer explicitly loads every face and weight the template uses with document.fonts.load, then asserts each one with document.fonts.check. If any check fails, the job fails loudly and retries, instead of producing a plausible-looking wrong image.

4. A layout guard runs last. Every text block in the template is checked for overflow by comparing its scroll size with its box. If a headline still does not fit, typically because the model wrote a longer line than the template expects, the renderer steps the font size down within the brand's limits and, if that is not enough, flags the post for review instead of shipping a clipped image.

A side effect of removing the network was speed. With the fonts inlined, I could also keep one browser alive and open a fresh context per job instead of launching Chromium for every render.

The outcome

I replayed the same 1,200 jobs from the original fortnight through the new renderer at the same concurrency, then watched production for the following four weeks.

Fallback-font renders went from 85 of 1,200 (7.1%) to zero, in the replay and in the four weeks after. Clipped headlines went from 41 to none shipped. Three posts were flagged by the layout guard for a human to shorten, which is what the guard is for. Median render time per image fell from 2.6 seconds to 1.1 seconds, about 58% faster, because no render waits on someone else's server any more.

The broader lesson is one I keep relearning. A flaky step in a pipeline is usually a race, and a race is fixed by removing one of the runners, not by giving the slow one more time. If a render can be made to depend only on what is already in memory, it stops being flaky and starts being fast at the same time.

Portrait of Jerome D'Mello
Jerome D'Mello

I build frontends that large teams can keep shipping. And the AI tooling that takes the repetitive work off them.

Start a conversation