Offline-first browser tools (module workers, precache lists, no build step)
web-workersservice-workerofflinees-modulespwaarchitecture
A pattern for heavy, privacy-friendly web tools that do everything client-side (nothing uploaded) and keep working offline, as built for cncfileoptimizer.com (2026-09/10): static ES modules with no framework, no build step and no CDN; a module Web Worker for computation; a service worker for offline.
Layering
- Engine: pure ES modules with no DOM access. The same files run in the worker, in Node tests
(
node --test) and in benchmark scripts, so correctness tests never need a browser. - Worker: a module worker (
new Worker(url, {type: 'module'})) that dispatches requests by type. - UI: plain modules for the store, panels, viewer and file handling.
- Settings UI generated from the engine's DEFAULTS (label, type, group, advanced, showIf), so a new engine option appears in the UI without hand-written form code. Presets are built-ins plus localStorage plus JSON import/export.
Worker protocol
- Request
{id, type, payload}; progress{id, 'progress', p, label}; then{id, 'result', result}or{id, 'error', message}. - Return large arrays as transferables (typed arrays for toolpaths:
x, y, z: Float32Array,k: Uint8Arraymove kind) to avoid copying. - Cancel = terminate and respawn the worker, then silently re-send the loaded document. Cooperative cancellation inside long synchronous loops is not worth the complexity.
- Keep worker requests stateless where possible (send the whole job each time).
Service worker and precache
- Network-first with an offline cache fallback, so a deploy is picked up immediately when online.
- Every file under the app must be in the precache list. Adding or removing a file without updating
PRECACHEbreaks offline loads; a unit test compares the list with the directory contents and fails the build. - Test offline for real: load, go offline, reload (the e2e smoke test does this).
Handing data between pages
Two entry pages shared work through sessionStorage ({kind, name, text} or base64 for binary), read
once by the receiving page. Versioned localStorage keys (app.settings.stl.v2) let defaults change
without stale saved values overriding them.
Durable browser state (agreed design for a game save system)
For saves that must survive, the plan agreed for Spacebar's JS rebuild: IndexedDB as the main store, a
synchronous localStorage "panic write" on pagehide/visibilitychange for the latest state, a
navigator.storage.persist() request so the browser does not evict the data under storage pressure, and
a schema version with explicit migrations from day one.
Deploy verification
The deploy script backs up the old docroot, rsync --deletes the app, then curls every file and checks
both the 200 status and the MIME type (a JS module served with the wrong type fails silently in the
browser). On an older Apache: a global Alias /icons/ hijacks app icons, so ship them under assets/,
and .webmanifest had no MIME type, so the manifest was named manifest.json
(Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)).
Testing
Node unit tests for the engine (144 tests, about 70 s), an e2e smoke test in headless Chrome via
playwright-core pointed at the system Chrome, runnable against local or live with a BASE_URL
variable, including 390 px screenshots (Headless Chrome testing gotchas).
See also
- Caching static sites and unbundled ES modules (?v= stamps, no-cache, prefetch)
- A production vanilla-JS single-page app (no framework, no build)
- Optimizing CNC G-code safely (arc fitting, simplification, self-checks)