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
| Requirement | Where |
|---|---|
| Web app manifest | web/manifest.webmanifest (application/manifest+json) |
| 192 and 512 icons, plus a maskable one | web/icons/ |
start_url, scope, display: standalone | the manifest |
| A service worker with a fetch handler | web/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:
| Request | Behaviour |
|---|---|
| navigation | fetch, 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-GET | never 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:
- the box shows the previous values, not a blank
- a small
offline · 12m agobadge sits in the corner and the widget gets a dashed outline - the snapshot key is the widget type plus a hash of its props, so two weather boxes for the same
- city share one entry
- the store is capped at 40 entries; the oldest is dropped first
- a failed write (quota, private mode) is ignored — caching is a bonus, never a requirement
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:
| Choice | Behaviour |
|---|---|
| Last state (default) | show the previous reading with the stale badge |
| Offline screen | show 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.
- delivery is server-side and encrypted (RFC 8291); the worker only parses the JSON payload
- Send test appears once subscribed and posts
/api/push/test, so the OS banner can be - checked without publishing a release
- a push click focuses an open Bento Spaces window or opens
/ - in-app notifications are independent: if push is unavailable or denied, the bell still works
- the setting is per browser, not per account; the same account can have several
- Firefox requires the permission prompt inside the click itself, so
main.jscalls Notification.requestPermission()before anyawait; waiting first can lose the gesture and- return
default. Firefox also refuses to subscribe until the worker isactive. - on localhost the worker is unregistered unless the page is opened with
?sw=1; Turn on - now starts it on demand, so the plain URL works too. A browser with an active push subscription
- keeps its worker across reloads; without one, the next plain load cleans it up. The panel still
- names
?sw=1if the worker cannot start. - on macOS, Firefox must also be allowed in System Settings → Notifications. A browser
- permission alone does not override a system-level block.
- once a browser permission is Blocked, the page cannot ask again; only the browser's site
- settings can undo it.
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
- the shell, dock, spaces and layout (layouts live in IndexedDB)
- every local widget: clock, note, countdown, QR, dice, converter, photo frame (cached images)
- anything already fetched and cached by the browser
- editing, moving, resizing, undo/redo
What degrades:
- connectors and the Gree AC show the last reading, marked stale, with controls disabled
- weather, feed, markets, holidays, crypto and air show their last state, marked stale — unless
- Offline widgets is set to Offline screen
- sharing, export and sign-in need the server
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:
- on
localhost/127.0.0.1without?sw=1: unregisters the worker, **unless this - browser has an active push subscription** — then it keeps it so notifications survive reloads
- with
?sw=1: registers it, so the offline path can be tested - on any other host: registers normally
# 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
- Chrome / Edge (desktop): install icon in the address bar, or ⋮ → Install Bento Spaces
- Android Chrome: ⋮ → Add to Home screen → Install
- iOS Safari: Share → Add to Home Screen. It opens standalone with the dark status bar.
- iOS applies its own rounding and ignores the maskable safe zone, which is why the art is inset.
- macOS Safari: File → Add to Dock (Safari 17+)
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 manifest is served as
application/manifest+jsonwith the required fields - every declared icon exists, is a PNG and matches its declared size
- the worker installs and controls the page
- with CDP network emulation offline, the app still boots from cache and applies cached CSS
- a live widget restores its last state with a stale badge, and the Offline screen setting
- replaces it with the offline message
- a plain
localhostload unregisters the worker again
The suite cleans up its registration and caches, even when it fails, so it cannot poison the other browser tests.