The 200KB decode nobody needed to redo
My OG-card generator bundles a fallback font so consumers don’t have to supply their own. It’s stored as a base64 string baked into the package, since satori’s font parser can’t read variable fonts and this sidesteps that entirely by shipping a static weight. Every time the library generated a card, it decoded that base64 string back into raw bytes. Every single time. Even when generating fifty cards in the same build, for fifty different blog posts, using the identical font.
The fix is one if statement
let cachedFonts: SatoriFont[] | null = null;
export async function defaultFonts(): Promise<SatoriFont[]> {
if (!cachedFonts) {
cachedFonts = [
{ name: 'Space Mono', data: Buffer.from(regularBase64, 'base64'), weight: 400, style: 'normal' },
{ name: 'Space Mono', data: Buffer.from(boldBase64, 'base64'), weight: 700, style: 'normal' },
];
}
return cachedFonts;
}
That’s the entire change. Decode once, cache the array, hand back the same reference every time after. The kind of fix that barely feels worth a changelog line.
The test that actually matters here
The easy version of this test calls defaultFonts() and checks the fonts look right: name, weight, that the data is a real Buffer with real content. Fine test, but it would pass identically whether or not the memoization worked, because it only ever calls the function once.
The test that proves anything calls it twice and checks identity, not equality:
test('defaultFonts memoizes - repeat calls return the identical buffer instances', async () => {
const first = await defaultFonts();
const second = await defaultFonts();
assert.equal(first, second, 'should return the same cached array, not a fresh one');
assert.equal(first[0].data, second[0].data, 'should return the same buffer instance');
});
assert.equal on two Buffers checks reference identity here, not byte-for-byte content equality the way assert.deepEqual would. Two freshly-decoded buffers with identical bytes would fail this test, and that’s exactly the point. The whole bug was “decodes the same bytes twice,” so a content-equality check would happily pass on the broken version too. The only test that can distinguish “memoized” from “not memoized, but consistent” is one asserting that both calls literally handed back the same object.
Why this one was worth doing
It’s a small fix for a small problem. A single build decoding a couple hundred KB of base64 an extra few dozen times isn’t going to make or break anyone’s day. But it compounds invisibly: a site with a hundred posts generating a card per post pays this cost a hundred times over for zero benefit, and nothing about the symptom (a slightly slower build) points a finger at the font decode. Cheap to fix, easy to miss, exactly the profile of bug worth clearing out of a backlog even when nothing is complaining yet.
Related reading
The escaping bug that only shows up with a second query param
A rewrite function that worked in every test and every real URL it had ever seen, and would still have broken the moment someone added a second query param.
Testing the part of the codebase that documents its own footgun
A README warning about a subtle failure mode is a confession that the code isn't tested against it yet.
Proving a fallback actually falls back
A test that passes whether or not your override option works is not a test, it's a coin flip that happens to land the same way every time.