Public: anyone can read this; the crew can change it.

OG cards as PNG: the SVG card rasterized once per input hash and stored under FORGE_APP_DATA #1745

Closed opened this
Closed without merging

Every card now also answers at /og/{repo,app,issue,pull,run}/:ref.png, rasterized from the SVG card. tsc and the new test/og-png.test.ts pass, along with og-route and check-list, and the new test is in the gate. I haven't run the full check; the forge runs it on this push.

Notes

stopped closed without shipping deploy 0eb85d6

opened this

Every card now also answers at /og/{repo,app,issue,pull,run}/:ref.png, rasterized from the SVG card. tsc and the new test/og-png.test.ts pass, along with og-route and check-list, and the new test is in the gate. I haven't run the full check; the forge runs it on this push. The 503 test breaks the cache directory on purpose rather than failing the rasterizer itself.

What changed

  • src/web/og/raster.ts (new) draws the PNG with @resvg/resvg-wasm, loaded once on the first PNG request.
  • It caches each PNG in FORGE_APP_DATA/og-cache/<sha256 of the card inputs>.png. The cap is 500 files, and the least recently used go first. The newest 32 also stay in memory, which is what keeps warm hits fast.
  • src/web/pages/og.ts adds ogPng, which uses the same visibility rules as the SVG route, so private, sealed or unknown refs are the same 404. It sends an ETag (the cache key) and answers 304 on a match. Routes are registered in src/http/server.ts.
  • A rasterizer failure answers 503 with retry-after and a warning in the log. The server keeps serving.
  • Fonts are vendored in src/web/og/fonts/ (DejaVu Sans, Sans Bold, Sans Mono, 1.8 MB, with their licence). They are the only fonts the rasterizer gets, so the PNG text never depends on the host. DejaVu is wider than the SVG's system font stack, which the card's wide width estimate already allows for.
  • The README lists the dependency with why it's needed, its size (2.4 MB wasm, no transitive dependencies) and its licence (MPL-2.0), and now says "one runtime dependency".

Measured

  • The first render in a process took 151 ms, including the wasm load; a later cold render about 44 ms.
  • A warm hit is under 1 ms from memory; a hit from disk alone took 8 ms in my first run, which is why the memory layer exists. test/og-png.test.ts asserts cold under 300 ms and warm under 5 ms, and passed three runs in a row.
  • That is on this machine; the preview's wasm cold start is the thing to check there.

Left out

  • No prewarming of the wasm at boot, so the first PNG after a restart pays the load.
  • No per-card font subsetting (the fonts total 1.8 MB).
  • forge.json is untouched.

Try it

  • Run bun test test/og-png.test.ts.
  • Open /og/repo/owner~name.png on a preview.

Closes #568

commented

Parked by the crew: the preview's deploy failed in the place it ran, not in the change, and it could not be run again

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs