Open the editor

An offline-first editor in plain JavaScript

IndexedDB, a small service worker and no framework: what worked, and what bit us.

Observability stack architecture diagram
Observability stack architecture diagram, one of the templates in this post

The ArchBoard editor is about 2,300 lines of plain JavaScript and SVG. It has no framework, no bundler and no runtime dependencies, and after your first visit it works with no network at all. This post explains how it is put together, and what we would warn you about if you try the same.

Why no framework#

A diagram editor is mostly a rendering engine: shapes, arrows, selection, dragging, zoom. Frameworks are good at forms and lists; they do little for an SVG canvas, where you end up managing elements yourself anyway. Without one, the editor loads fast, and the code you read is the code that runs.

The structure is deliberately simple. Each file adds itself to one global namespace, window.AB: AB.store for the project, AB.canvas for the board, AB.icons for the icon library. Script tags load them in order. It even runs when you open the HTML file straight from disk, which made early development very quick.

Data: IndexedDB, with a fallback#

Everything you draw is stored in IndexedDB, in two object stores: one for the project and small settings, one for cached icon SVGs. A thin wrapper exposes get, set and keys.

Some private browsing modes block IndexedDB. Rather than failing, the wrapper falls back to in-memory maps: the editor still works, it just forgets on reload.

Saving is automatic. The project is written every few seconds when something changed, when the tab is hidden, and before the page unloads. If the tab closes unexpectedly, you lose a few seconds of work at most.

The app shell: a small service worker#

The service worker caches a fixed list of files, the HTML, CSS and JavaScript of the editor, and nothing else. Its strategy is "stale while revalidate": answer from the cache immediately, then fetch a fresh copy in the background for next time.

e.respondWith(caches.open(CACHE).then(async (cache) => {
  const hit = await cache.match(key);
  const fresh = fetch(e.request).then((res) => {
    if (res.ok && res.type === 'basic') cache.put(key, res.clone());
    return res;
  }).catch(() => hit || Response.error());
  return hit || fresh;
}));

Two details keep it safe: only listed paths are cached, so random URLs cannot grow the cache, and the cache key ignores the query string, so ?v=2 cannot bypass it.

Icons: 657 SVGs, fetched once#

ArchBoard ships 657 technology icons from the open-source Devicon project. Bundling all of them into the page would make the first load heavy, so the editor downloads the list from a pinned CDN version and stores each SVG in IndexedDB the first time it is shown. In the background, a few workers quietly fetch the rest, so after one online visit the whole library is available offline.

Pinning the version matters: the icon set only changes when we decide, so a CDN update cannot change your diagrams.

What bit us#

Stale caches during development. With a cache-first service worker, the browser happily serves yesterday's CSS while you wonder why your change does not show. Bumping the cache name on every release, and clearing the service worker while developing, became routine. If you take one thing from this post: version your cache, and make bumping it part of every release.

Moving the editor. When the editor moved from / to /app/, every relative path changed meaning, including the service worker's file list and the path to bundled icons. One security check rejected any path containing .., which was suddenly part of a legitimate path. Tests that load the real page would have caught it sooner.

No types. Plain JavaScript is fast to write and easy to break. Adding // @ts-check with JSDoc comments, which needs no build step, is the next step we would recommend to anyone working this way.

Would we do it again?#

Yes, for this kind of product. The editor loads quickly, works offline, has nothing to patch for dependency vulnerabilities, and fits in your head. The cost is discipline: cache versions, careful paths and tests that a framework or bundler would otherwise force on you.

If you want to see the result, open the editor, load a template, then turn off your network and keep working.