Every post on this site has its own link preview: a 1200 by 630 card with the post’s date, reading time, title, description and tags, the image a chat app or a social site shows when someone pastes the link. Nobody draws them by hand. Two libraries do it: Satori turns a tree of boxes and styles into an SVG, and resvg turns the SVG into a PNG.

The natural home for that code is an Astro endpoint that returns a PNG, prerendered once at build time. On Cloudflare, that endpoint doesn’t build. Mine are drawn by a Bun script that runs before astro build starts, and it’s the piece of this site I’d copy into any Astro project on Cloudflare.
The endpoint that doesn’t build
Here is the smallest version I could write, a twelve-pixel gold square:
import { Resvg } from "@resvg/resvg-js"
import type { APIRoute } from "astro"
export const prerender = true
export const GET: APIRoute = () => {
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12"><rect width="12" height="12" fill="#c9973f"/></svg>`
const png = new Resvg(svg).render().asPng()
return new Response(png, { headers: { "Content-Type": "image/png" } })
}astro build stops while it builds the server. With the paths shortened and the output trimmed:
[WARN] [vite] Unexpected Node.js imports for environment "prerender". Do you need to enable the "nodejs_compat" compatibility flag?
- "path" imported from ".../@resvg/resvg-js/js-binding.js"
- "fs" imported from ".../@resvg/resvg-js/js-binding.js"
- "child_process" imported from ".../@resvg/resvg-js/js-binding.js"
[ERROR] [vite] ✗ Build failed in 734ms
[UNLOADABLE_DEPENDENCY] Could not load .../@resvg/resvg-js-darwin-arm64/resvgjs.darwin-arm64.node
- stream did not contain valid UTF-8 in .../@resvg/resvg-js/js-binding.js at 2588..2618The word to notice is “prerender”. With prerender = true, I expected the page to be built in Node and saved as a file. The Cloudflare adapter does save it as a file, but by default it renders it inside workerd, the runtime behind Cloudflare Workers. The adapter’s prerenderEnvironment option says so: its default is "workerd". So a prerendered page is bundled for Workers like everything else.
resvg doesn’t survive that bundle. @resvg/resvg-js is a native Node addon: its JavaScript picks a compiled .node file for your platform and loads it. The bundler, building for Workers, tries to read that file as JavaScript, and “stream did not contain valid UTF-8” is the bundler finding machine code instead. Turning on nodejs_compat quiets the warning about fs and child_process. The build still fails, on the same .node file.
Satori on its own does no better. It has no native binary, but it lays out with Yoga and shapes text with HarfBuzz, both compiled to WebAssembly, and it compiles them when it first runs. A prerendered endpoint that returns Satori’s SVG builds, then fails when Astro renders it:
failed to asynchronously prepare wasm: CompileError: WebAssembly.instantiate(): Wasm code generation disallowed by embedderworkerd won’t compile WebAssembly handed to it as bytes. So in the default setup, neither half of the recipe runs where Astro wants to run it. I first hit the resvg half in April, and ran both again on Astro 7 while writing this.
Draw them first
The fix is to keep both libraries out of Astro entirely. A plain Bun script draws every card before Astro starts:
"build": "bun scripts/gen-og.ts && astro build && bun scripts/search-index.ts"The script writes PNGs into public/og/, and Astro copies public/ into the build as it is, like a favicon. Nothing the Worker bundles imports Satori or resvg, so nothing has to bundle them, and Bun loads the native addon like any other package. public/og/ is in .gitignore, and the script empties it before it draws, so a card whose page is gone doesn’t outlive it.
The drawing itself is short. This is the core of it, cut down to one font and one title:
import { readFileSync, writeFileSync } from "node:fs"
import { fileURLToPath } from "node:url"
import { Resvg } from "@resvg/resvg-js"
import satori from "satori"
// Satori reads TTF, OTF and WOFF, not WOFF2: Fontsource ships a .woff
// beside every .woff2, so read the site's own font from node_modules.
const font = (file: string) =>
readFileSync(fileURLToPath(import.meta.resolve(`@fontsource/${file}`)))
const fonts = [
{
name: "Cormorant Garamond",
data: font(
"cormorant-garamond/files/cormorant-garamond-latin-400-normal.woff"
),
weight: 400 as const,
style: "normal" as const,
},
]
// A satori tree: plain objects, the shape a JSX element compiles to
// (key included: React's types, if you have them, require it).
const card = {
type: "div",
key: null,
props: {
style: {
display: "flex",
alignItems: "center",
width: "100%",
height: "100%",
padding: 72,
backgroundColor: "#191614",
color: "#eeeae2",
fontFamily: "Cormorant Garamond",
fontSize: 92,
},
children: "My Blog Was 672 Pixels Wide on a 430-Pixel Phone",
},
}
const svg = await satori(card, { width: 1200, height: 630, fonts })
const png = new Resvg(svg).render().asPng()
writeFileSync("card.png", png)bun scripts/draw.ts writes card.png, 1200 by 630, with the title set in the site’s serif. The real script does the same for every card, and a post’s page names its card in the head: og:image is /og/writing/<slug>.png, with the card’s size and the card’s words as its alt text.
WOFF, not WOFF2
The one surprise in that code is the font file. Satori’s README is plain about it: “Satori currently supports three font formats: TTF, OTF and WOFF. Note that WOFF2 is not supported at the moment.” The site’s pages load WOFF2, like most sites. But Fontsource, where the site’s fonts come from, ships a .woff beside every .woff2, so the script reads the .woff files straight from node_modules with import.meta.resolve. The post cards get the same fonts, from the same packages, as the pages.
A font without a character is the other quiet failure. Satori draws the character in another font you gave it, or, when none has it, as an empty box. It also hands the characters it couldn’t find to a loadAdditionalAsset function, which makes a good test: the script runs each post’s title, description and tags through it, each with its own font alone, and names any character that font can’t draw.
The card and the page must agree
The script runs outside Astro, so it can’t ask Astro’s content collections for the posts. It reads the .mdx files itself, with gray-matter. Two readings of the same files can drift apart, and a card can fail where its page doesn’t, so three things keep them together.
- One rule for what’s live.
isPublished()insrc/lib/published.tssays a post is live when it isn’t a draft and its date has come. Every page that lists posts uses it, and so does the script, so a post scheduled for a Monday gets its card on the Monday build that publishes it. - A check before drawing. The script checks every post that will go live, scheduled ones too. A blank title or description stops the build, naming the post. A title too long for three lines is cut and reported as a warning. That runs on the push that queues a post, while I’m watching, not first on the Monday morning that publishes it.
- A check after the build.
check-og.tsreads every page in the build. Eachog:imagemust name a file the build holds, at the size the page declares, and each post’s alt text must be its card’s words as the script read them. CI runs it on every branch but main, and the deploy runs it on main before uploading, so a mismatch leaves the live site as it was.
Satori is not Chrome
The cards were designed as HTML boards and drawn in Chrome, so I could hold Satori’s output up against them. Satori’s layout is close to Chrome’s, not equal:
- It rounds its boxes to whole pixels, where Chrome keeps fractions of a pixel along a line and paints text at fractional positions. Both put a baseline on a whole pixel, but they round at different steps, so a line can land a pixel apart.
- It lays a line out in pieces and drops a negative letter-spacing at the end of each one. On the post card’s title, that put 3.22 px more after every word than Chrome does, and “Keys and Types the Database Can Actually Check” took three lines in Satori where Chrome needed two.
- It keeps ligatures that Chrome turns off under letter-spacing.
So the script takes Satori’s measurements and does the placing itself. It works out where Chrome would break each line and set each baseline, from a small module of Chrome’s arithmetic pinned by tests, and places each word and mark of a title, and each line of a description, as its own absolutely positioned box. Measured against its board in Chrome, the post card’s worst offset is 0.09 px.
You only need this if you have a design to match. But “nearly right” is exactly when it’s worth measuring: none of these differences shows until a title wraps one line sooner than the design did.
The other way out
Since version 13.1, in March 2026, the Cloudflare adapter can prerender in Node instead:
adapter: cloudflare({ prerenderEnvironment: "node" }),I tried it while writing this. The endpoint builds, with Satori and resvg together, and writes its PNG. If you’re starting from nothing, it’s the shortest path.
I’m keeping the script anyway. The option moves every prerendered page to Node, not just the cards, and the script changes nothing about how the rest of the site builds. The script also checks a scheduled post’s card on the day I queue it, without putting that card in the build. An endpoint draws what it publishes, so it would meet the post on the Monday it goes live, or publish its card early. The cost is astro dev: it doesn’t run the script, so a new post’s card appears after the next build.
The rule
On Cloudflare, by default, even a prerendered page is rendered by a Worker. Anything that needs a native binary, or WebAssembly compiled on the fly, runs before astro build, as a script that writes into public/. Then check the build, because a broken link preview fails quietly: the first person to see it is whoever you sent the link to.