{
  "version": "https://jsonfeed.org/version/1.1",
  "title": "Dustin Edwards blog: svg",
  "home_page_url": "https://dustinedwards.info/writing/tags/svg",
  "feed_url": "https://dustinedwards.info/writing/tags/svg/feed.json",
  "description": "Posts tagged svg.",
  "language": "en-US",
  "authors": [
    {
      "name": "Dustin Edwards",
      "url": "https://dustinedwards.info"
    }
  ],
  "items": [
    {
      "id": "https://dustinedwards.info/writing/observable-plot-inside-a-worker",
      "url": "https://dustinedwards.info/writing/observable-plot-inside-a-worker",
      "title": "Observable Plot charts in Cloudflare Workers",
      "content_text": "\nCan Observable Plot render charts inside a Cloudflare Worker? Yes. Workers have no DOM, but Plot accepts a `document` option, and linkedom, a small DOM written in JavaScript, fills that role. This site draws every chart in its posts that way, as inline SVG in the HTML.\n\nA chart on this site is complete before any script runs. It arrives with a written description, a caption and a data table, so screen readers and AI crawlers read the same numbers a sighted reader sees.\n\n*Part 1 of [Academic site design](/writing/series/academic-site-design). Next: [Designing a color palette the build can check](/writing/color-palette-the-build-can-check). Related: [Building on Cloudflare](/writing/ten-years-on-cloudflare), the series on the platform this runs on.*\n\n## How do you run Observable Plot without a DOM?\n\nPass Plot a document from linkedom. Plot builds its SVG through d3-selection against whatever `document` it's given, and linkedom's `parseHTML` returns one that works in a Worker.\n\n```js\nimport * as Plot from \"@observablehq/plot\";\nimport { parseHTML } from \"linkedom\";\n\nconst { document } = parseHTML(\"<html><body></body></html>\");\nconst svg = Plot.plot({\n  document,\n  marks: [Plot.barY(data, { x: \"library\", y: \"kib\", fill: \"var(--chart-1)\" })],\n}).outerHTML;\n```\n\nThe result is a string of SVG markup, ready to put in a page. This site runs Plot 0.6.17 with linkedom 0.18.14. It calls Plot through Enarratio 0.13.1, a chart library I wrote that uses Plot and adds the text alternatives and the optional browser layer.\n\n## Why render charts on the server?\n\nA research site's charts are content. A chart drawn in the browser is missing until its script runs, and anything that reads HTML without running script sees nothing. That includes AI crawlers that fetch pages without a browser.\n\nServer-rendered SVG also follows the site's light and dark themes. Plot accepts `var(--chart-1)` anywhere it takes a color and passes the string through untouched. The SVG sits inline in the page, so the variable resolves against the site's stylesheet, and one stored render works in both themes. The chart colors then fall under the same contrast check as the rest of the palette ([color palette post](/writing/color-palette-the-build-can-check)).\n\n## How a chart gets into a post\n\nThe data is a CSV file in the site's media library, and the post cites it with a directive. At render time, the Worker reads the CSV from R2 and Enarratio draws the chart.\n\n:::diagram{title=\"How a chart in a post becomes inline SVG on dustinedwards.info\" alt=\"Flow diagram in five steps. The author uploads a CSV file to the media library. The post cites it with a chart directive naming the file. At render time the markdown pipeline reads the CSV from R2. Enarratio draws the chart with Observable Plot, using linkedom as the DOM, and returns HTML: an SVG with role img and an aria-label, a caption and a data table. That HTML is stored with the post. In the browser an optional island adds tooltips, keyboard navigation and downloads.\"}\n```mermaid\nflowchart LR\n  a[CSV in media library<br>/media/key.csv] --> b[Post cites it<br>chart src]\n  b --> c[Render reads CSV<br>from R2]\n  c --> d[Enarratio + Plot<br>linkedom as DOM]\n  d --> e[Stored HTML<br>SVG + caption + table]\n  e --> f[Optional island<br>tooltips, keyboard]\n```\nSteps 3 to 5 run in the Worker when a post is saved, and in Node during a build.\n:::\n\nThe directive looks like `:::chart{src=\"/media/<key>.csv\"}`. A `src` that isn't a CSV in the media library is refused with an error naming the fix. The render also refuses a chart with no title or with too many series. Because the data is a file, a reader can download the exact CSV behind each chart.\n\n## Which charting libraries work in Cloudflare Workers?\n\nOf the three I tested on July 31, 2026, Observable Plot with linkedom and Apache ECharts both ran, and Plot's bundle was the smaller of the two by 2.5 times. Vega-Lite fails at runtime.\n\n:::chart{type=\"bar\" x=\"setup\" y=\"gzipped_kib\" y-unit=\"KiB\" alt=\"Bar chart of gzipped Worker bundle size in one probe harness on July 31, 2026, built with Wrangler 4.116.0. Observable Plot with linkedom was 254 KiB. Vega-Lite was 412 KiB but cannot run in a Worker. Apache ECharts in its server-side rendering mode was 634 KiB.\" title=\"Gzipped Worker bundle size by charting setup, July 31, 2026\" source=\"The author's Worker bundle probe\"}\n```csv\nsetup,gzipped_kib\nPlot + linkedom,254\nVega-Lite,412\nECharts SSR,634\n```\nOne probe harness, July 31, 2026. The production Worker's Plot and linkedom share measured 226 KiB gzipped the same day.\n:::\n\n| Setup (probe harness, Jul 31, 2026) | Gzipped bundle | Runs in a Worker? |\n|---|---|---|\n| Observable Plot + linkedom | 254 KiB | Yes |\n| Vega-Lite | 412 KiB | No: `EvalError`, code generation from strings |\n| Apache ECharts, SSR mode | 634 KiB | Yes |\n\nVega compiles its expression language with runtime code generation, and Workers forbid generating code from strings. ECharts has a server-side mode that needs no DOM at all, so I expected it to be smallest. It came out the largest of the three. In production the same day, Plot and linkedom added 930 KiB raw and 226 KiB gzipped to the Worker.\n\ndomino, the DOM the Plot team used to show Plot running in a browser Web Worker, can't be bundled for Workers. Its source uses `with` statements, which strict-mode ES modules forbid. esbuild stops with five errors of the form \"With statements cannot be used with the \"esm\" output format due to strict mode.\"\n\n## How do you make an SVG chart accessible?\n\nPut `role=\"img\"` and an `aria-label` on the `<svg>` itself, give the figure a caption, and put an equivalent data table next to it. The description and table carry the numbers for anyone who can't see the chart.\n\nDon't put `role=\"img\"` on the `<figure>`. That role makes every descendant presentational, so it would hide the caption and the data table from assistive technology. On this site the label on the SVG reads like \"Bar chart of gzipped Worker bundle sizes measured in the probe: Observable Plot with linkedom 254 KiB...\". Enarratio writes one from the data when the author gives none.\n\n## Is the SVG output deterministic?\n\nIn my July 2026 test, yes. Two hundred renders in one process gave one distinct output by SHA-256, and Node and workerd produced identical bytes.\n\nThat held only when both sides used the same DOM. My first Node run used a different DOM than the Worker, and the hashes differed because the two serialize differently. I'd check this again after any Plot or linkedom upgrade, since a chart that renders differently in the build and in the Worker shows up as drift on every deploy.\n\n## What does the browser add?\n\nAn optional island, loaded only on pages that contain a chart. It adds tooltips, keyboard navigation through the data points, legend filters and downloads, and it redraws each chart to the width of its column.\n\nThe island was 28.9 kB gzipped on November 14, 2026 (my measurement with `gzip -9`), and pages without a chart never load it. The chart's own HTML in the Observable Plot post, SVG and data table included, was 1.9 kB gzipped.\n\n*Next: [Designing a color palette the build can check](/writing/color-palette-the-build-can-check).*\n",
      "summary": "Render Observable Plot charts as inline SVG inside a Cloudflare Worker with linkedom, with a caption and data table for screen readers and AI crawlers.",
      "date_published": "2026-07-31T00:00:00.000Z",
      "date_modified": "2026-12-04T16:36:00.000Z",
      "tags": [
        "accessibility",
        "cloudflare-workers",
        "data-visualization",
        "observable-plot",
        "svg"
      ]
    }
  ]
}
