Bento Spaces docs

Bento Spaces documentation

15 — PWA and offline

Bento Spaces installs as an app and opens with no network. This document is the contract: what is cached, what is not, and why the dev server behaves differently from a real deployment.

What makes it installable

RequirementWhere
Web app manifestweb/manifest.webmanifest (application/manifest+json)
192 and 512 icons, plus a maskable oneweb/icons/
start_url, scope, display: standalonethe manifest
A service worker with a fetch handlerweb/sw.js, scope /
HTTPS (or localhost)the host

The head also carries theme-color for both colour schemes, apple-touch-icon, and the iOS apple-mobile-web-app-* metadata so “Add to Home Screen” opens without Safari chrome.

The brand lives in web/logo.svg — one file for the header, the favicon and the install icons. The PNGs are rasterised from it:

node scripts/make-icons.mjs
# or: CHROME="/path/to/chrome" node scripts/make-icons.mjs

That writes icon-192.png, icon-512.png, maskable-512.png and apple-touch-icon.png. Chrome is a build-time tool here, not a Bento Spaces dependency; the PNGs are committed, so a checkout only needs this script when the logo changes. The any icons keep transparency. The maskable icon puts the art at 78% on a full-bleed #383838 background, so Android can crop it to a circle without clipping a tile; apple-touch uses 88% for the same reason.

Cache strategy

web/sw.js is deliberately network first:

RequestBehaviour
navigationfetch, cache a copy of the shell, fall back to cache when offline
same-origin static (/src/*.js, CSS, icons, manifest)fetch, refresh the cache, fall back to cache on failure
/api/*never intercepted
non-GETnever intercepted
cross-origin (widget data hosts)never intercepted

Network first, not cache first, because assets are not content-hashed. A cache-first worker would pin a user to an old app.css or an old main.js until somebody remembered to bump a constant. Offline still works because every successful response is written to the cache; the second visit is already complete.

The precache list is small on purpose: shell, CSS, manifest and the two install icons. JavaScript modules enter the cache the first time the app loads, which keeps install fast and avoids a list that drifts out of date.

CACHE is a version string (bento-shell-v2). A new worker activates immediately (skipWaiting + clients.claim) and deletes older bento-shell-* caches. Bump it when the precached set changes; a new deploy does not need a bump because the strategy refetches.

Offline widgets: last state by default

Widgets that need the network keep their last successful reading in localStorage (bento.snapshot.<type>.<propsHash>), and render it when a refresh fails:

For connector widgets the same rule applies: the Gree card keeps the last temperature and power, marks itself stale, and disables the toggle. Acting on a stale reading would be a guess.

The other option: an offline screen

Settings → Offline widgets:

ChoiceBehaviour
Last state (default)show the previous reading with the stale badge
Offline screenshow the widget's offline message instead of old values

Changing the setting re-renders the shelf immediately, so it applies to boxes already on screen.

Only built-in widgets that fetch their own data use snapshots. Declarative and sandboxed module widgets keep their own behaviour.

Browser push

The service worker also shows release notifications. From the bell's Preferences, Browser notifications → Turn on asks for permission, subscribes with the server's VAPID key and posts the endpoint and keys to /api/push/subscribe. Turning it off unsubscribes locally and deletes the row.

Real delivery was verified on Firefox/macOS against Mozilla's push service: the request is accepted with 201 and the OS banner appears. The fake transport still covers the encrypted body in api-smoke.

What works offline

What degrades:

This is a shell cache, not an offline sync engine. Edits made while offline are stored locally as usual; signed-in spaces do not queue writes for later (see Roadmap).

The dev-server gate

Registering a service worker on localhost makes development confusing: the first reload can serve a cached shell from before the edit. main.js therefore:

# offline check on a dev machine
open http://localhost:5080/?sw=1
# reload once so the worker controls the page, then go offline and reload again

Service workers need a secure context anyway: a plain-HTTP LAN address cannot register one, so a self-hosted Bento Spaces over http://192.168.x.x simply stays online-only. Put it behind HTTPS (Tailscale, Cloudflare, Caddy) if you want install and offline there.

Installing

There is no background sync and no share-target. The goal is a fast, offline-capable webtop, not a notification client.

Verification

node scripts/e2e/pwa.e2e.mjs checks, against a real browser:

The suite cleans up its registration and caches, even when it fails, so it cannot poison the other browser tests.