The Occasional Jam No. 12

No. 12 devlog

Drawing Share Cards at the Edge

3 min read

Paste a result link into a group chat and your friend sees a picture before they see the link.

Sharing is the reason this site exists, so that picture matters more than most of what I build. Nobody attaches it. The chat app fetches the page behind the link, reads the few tags in its head that say how to preview it, and fetches the image one of them names. That whole move is an What a chat app does with a pasted link: it fetches the page, reads its preview tags, and shows a card with a title and a picture..

A shared result carries itself inside its own link. The game and the date sit in the path, and the points, the tier, and the game number ride in encoded text after the question mark. Nothing is stored when you share, and nothing is looked up when your friend arrives.

A shared result, taken apart

/r/{game}/{date}?s=… The page a friend lands on. The score is the part after ?s=.
Nothing in the link is an identifier. There is no stored record for one to point at.

That is cheap and private, and it leaves no image file for me to hand over. So the first version drew one per score, on request, in the second between the chat app asking and the message appearing.

The first version drew every card at the edge

Almost every page here is a static file. The few that are not run as small functions at Cloudflare's servers near whoever is asking. Code there runs in a small sandbox with no browser and strict limits., so the card had to be drawn there, with no browser to draw it in. The code described the card as a tree of boxes, one library laid that out into a vector drawing, and a second painted it into a 1200 by 630 picture. The plan counted on caching: draw each link once and keep the picture for a year.

For the builders: what the sandbox made me do
  • Satori did the layout and resvg, compiled to WebAssembly, did the pixels.
  • The runtime will not compile WebAssembly from fetched bytes, so the module shipped inside the deployed code through a build plugin.
  • The build's route analysis runs in plain Node, which cannot resolve that import, so the endpoint loaded the renderer with await import(...) inside the handler.
  • Fonts were fetched from the site's own origin on first use and kept in memory, to keep them out of the bundle size limit.
  • Satori's layout engine also ships as WebAssembly. Its pure JavaScript build needed no special handling, so that is the one I used.

It did not fit

It worked on my laptop. In production, most first draws died. Cloudflare's free plan allows 10 milliseconds of CPU per request, and a card took 350 to 550, measured warm across every game. The platform killed the function before my own fallback could answer, so the chat app got a bare error and the share unfurled with no picture at all. In one test, 22 of 25 fresh links came up empty.

One card needed 35 to 55 times the CPU the free plan allows a request.
One card needed 35 to 55 times the CPU the free plan allows a request.
ItemValue
The free plan's limit10 ms
One card, fastest draw350 ms
One card, slowest draw550 ms

The cache never saved it either. Production never cached a card, so every link paid for its own draw. And no cheaper drawing would close a gap that size: the cost was the layout and the painting, not anything the design added. The paid plan allows 30 seconds for $5 a month. I went the other way.

A chat app fetches a link's picture once, when it is pasted. Each failure was a share lost for good.

Now every card is a file

The same drawing code runs on my laptop now, ahead of time. It draws five cards per game: one for each of the four tiers, and one for links older than tiers. A shared link points at its game's card for its tier, which is a plain file and costs the server nothing to send.

A share card. On the left, the JamCatWow name, the game's name, the words Think you can beat it?, and a row of four tier stickers with the third one lit. On the right, a sample board with letter tiles and a Play button.
  1. The game's name.
  2. The same line on every tier's card.
  3. The tier ladder, with the sharer's rung lit.
  4. A sample board for the game, never that day's puzzle.
Every share at this tier, in this game, unfurls into exactly this picture.

The card gave up the number and the points. They moved to the link's title, which chat apps print under the picture. On X, which shows only the picture and the domain, the score is not in the unfurl at all, though the share text above the link still has it.

The title under the picture

{Game} #96 · Cat · 210 points · Beat it on JamCatWow

Links shared before the switch still work: their old drawing address now redirects to the right card. A link whose score does not validate gets the site's own card, with nobody's score on it. And a test fails if a card's design changes without the files being redrawn.