Skip to content

ADR-0005: Client-rendered, baked pill-appearance thumbnails

  • Status: Accepted
  • Date: 2026-07-21
  • Supersedes: None
  • Superseded by: None

Context

The medication UI calls for photorealistic pill imagery that a user can tune per medication (shape, colours, finish, imprint, score, dimensions). A 3D renderer (Three.js) produces convincing results, but the timeline shows many pills at once and the app is server-rendered and local-first. Three approaches were assessed:

  • Live WebGL per row — one WebGLRenderer (and GL context) per thumbnail. Browsers cap WebGL contexts (~16), so a grid of live canvases exhausts them, costs GPU/memory, and pops in only after hydration (no SSR image).
  • Headless server bake — render PNGs on the server. Needs headless WebGL (SwiftShader) in the Node runtime; finicky and heavy for a single-user app.
  • SVG only — cheap and SSR-clean but not photorealistic.

Decision

  • Appearance is a parametric PillAppearance (see src/lib/pill/appearance.ts) persisted as JSON in a new nullable medications.appearance column (migration 0004), alongside the sha256 of a baked thumbnail (thumbSha). The optional tabletGeometry is reserved for a verified construction detail of an oblong pressed tablet — domed-oblong for a biconvex belly, barrel-oblong for a rolled cylinder such as Concerta. Absent data renders the flat-faced caplet, and the ŠÚKL prose parser never guesses a construction from a generic outline word.
  • The editor and the list's hidden first-bake queue use PillFoundry.svelte. It is dynamically imported on mount, so Three.js never lands in the SSR/main bundle; the list loads it only while a record lacks a thumbnail. The queue processes one medication at a time, and the camera auto-frames the pill so it fills the thumbnail.
  • On save, the editor's own canvas exports a PNG (toDataURL); the client posts it to ?/setAppearance, which stores it in the existing content-addressed immutable file store and folds the sha into the appearance JSON. No headless WebGL, no per-row GL contexts.
  • Rows and heroes render <img> from /pill/[sha] (a dedicated endpoint — /files/[sha] is for user documents and requires a documents row). When a medication has no baked thumbnail, PillThumb falls back to a deterministic SVG render.
  • Defaults are derived heuristically from form/kind/dose and a name hash; a medication with no appearance is auto-baked once, off-screen, when it first appears on the medication list or its detail page.

Consequences

  • Photorealistic thumbnails everywhere, SSR-clean, with Three.js isolated to one route chunk and exactly one GL context (the editor), created only on demand.
  • Thumbnails are immutable and deduplicated by sha; deleting a medication does not delete the blob (consistent with the store's contract).
  • Images are illustrative only and must not be used to identify medication; the editor states this and imprint/packaging remain the source of truth.
  • Client-GPU rendering means a thumbnail can vary slightly between devices; this is acceptable for an illustrative image.
  • Adds three as a direct dependency (client-only, code-split).