Skip to content

Offline operation in the browser

An app that declares offline data (specs/ui/offline.yaml or its code twin) works offline in the browser too — with the Vaadin renderer and with the React ones (Primer, SAP Fiori, Salesforce): the same screens as on the phone — listings, search and filters, opening a record, creating, editing and deleting — answered from a copy kept in the browser when the server cannot be reached, and the same sync when it can. Nothing to configure on the page: the renderer reads AppDto.offline and does the rest.

  1. First online visit. The app shell says it has offline data. The renderer loads its offline client — a separate chunk, never part of the page’s first load — opens the browser’s copy for this UI and this user, downloads the offline manifest, the screens’ packs and the data, and the header shows the sync indicator (Synced), drawn with the renderer’s own widgets (a header button in Primer, an item of SAP’s shell bar, a global action of Salesforce’s header).
  2. Online, the server is always asked first. Every answer still comes from the server; the client only keeps what it needs for later (the screens’ structure, never a record’s values outside the dataset copy).
  3. No connection — the browser says it is offline, or a request cannot reach the server — and a screen the copy covers is answered in the browser: the list, search, a record, the create and edit forms, saving (recorded as a change), deleting. Other actions say Needs a connection. The connection banner says how old the data is (“Offline — data from 2 h ago”) and whether the record on screen is Not synced; changed rows of a listing carry a Not synced mark.
  4. Reloading or opening the page with no network works: the page itself comes from the service worker, the app shell and the screens from the copy.
  5. The connection comes back: the changes are sent in order, then what changed on the server is pulled. Conflicts wait for the user in the Sync panel (the indicator opens it), where Resolve shows the two versions side by side (RecordMerge).
DatabaseSQLite compiled to WebAssembly (the official @sqlite.org/sqlite-wasm, Apache-2.0 / public domain) in a dedicated worker, on the Origin Private File System through SQLite’s opfs-sahpool VFS. The same store, tables and queries as the phone (SqliteOfflineStore in libs/mateu). No Cross-Origin-Opener-Policy / Cross-Origin-Embedder-Policy headers are needed.
One per…origin + UI + owner. The owner is the signed-in person read from the sign-in’s id token or the Bearer token (issuer + subject + tenant), or anonymous for a UI with no sign-in. Another person signing in on the same browser opens another copy and never sees the first one’s.
TabsAll the tabs of a browser share the database. The storage is held by one tab at a time (a Web Lock) only while it works, so every tab reads and writes it. Only one tab — the leader — syncs; the others ask it, and every tab refreshes when another changed something (a BroadcastChannel).
FallbackWhen the browser cannot run the worker (no OPFS, no Worker, a CSP without wasm-unsafe-eval): the copy is kept in IndexedDB as one sealed snapshot, read into memory. Fine for the few thousand records offline datasets are for. Because each tab would hold its own snapshot, only one tab of the browser works offline for a UI at a time; the others work online only.
FilesA photo or file chosen offline (fileFields) is kept in IndexedDB, sealed, and uploaded at the next sync through the same upload path as online.

The browser has no keychain and no SQLCipher. Each database gets two non-extractable WebCrypto keys, generated in the browser and kept in IndexedDB — the page can use them, never read them:

  • AES-GCM (256 bit) seals every stored value: the record, its base, the server’s copy, the change log, the metadata — each value with its own random IV;
  • HMAC-SHA-256 replaces the values of the indexed fields (the pack’s indexFields, e.g. a status) with a keyed digest, so the index still finds equal values without holding them. It still tells which records share a value of an indexed field, never the value.

Removing the copy deletes its keys first; whatever is left of it can no longer be read.

  • The copy is opened only for the person it belongs to. Signing out leaves it on the browser, locked, until the same person signs in again (pending changes are never lost); datasets declared wipeOnSignOut lose their records on sign-out.
  • A sign-in the page cannot name (a framework cookie session, an opaque token) gets no offline copy: the UI works online only. A copy that cannot be matched to a person could be shown or sent as somebody else.
  • A dataset the server’s manifest no longer lists for the caller is wiped (revocation), as on the phone.

Mateu already serves every UI as a Progressive Web App with a network-first service worker that caches the page and its static files and never the Mateu API. Offline operation relies on it: the offline client, its worker and the SQLite .wasm are static files under the renderer’s assets/, cached on the first online visits (the client hands them to the service worker as soon as they are loaded). The React renderers (Primer, SAP Fiori, Salesforce) load parts of a screen lazily (a date picker, a select); for an app that works offline their client hands the service worker EVERY file of the renderer (the list assets/mateu-<renderer>-assets.json is written when the renderer is packaged), so a form never opened online still opens offline. The data never goes through the service worker’s cache — it lives in the database above. A new build gets a new cache (the worker’s ?v=), so a deploy is never hidden.

Turning the PWA off (mateu.pwa.enabled=false) leaves offline operation working while the page is open, but a page opened or reloaded with no network will not load.

Under a CSP, add what WebAssembly and the worker need:

script-src 'self' 'wasm-unsafe-eval';
worker-src 'self';

Without 'wasm-unsafe-eval' the browser refuses to compile SQLite: the client falls back to the IndexedDB copy (one tab at a time) and logs it once.

The client follows the page’s public base: the database names, the worker and the API calls are under it. A renderer served from a CDN works offline the same way, as long as the CDN serves the .wasm with Content-Type: application/wasm.

  • Vaadin (the default web renderer): everything on this page.
  • Primer, SAP Fiori (UI5), Salesforce (SLDS) — the previews built on the React Native core: everything on this page. They run the phone’s own offline path (the core answers a covered request from the session when the server cannot be reached) over the same browser storage, encryption, leader tab and service worker as Vaadin, and open the same copy of a UI: switching renderers keeps the data and the pending changes. The sync indicator sits with the shell’s header controls, the offline line (“Connect once to download the data”, “Not synced”) under the header, the Sync panel and its RecordMerge in the design system’s own dialog and controls.
  • Redwood (Oracle Visual Builder): not yet — it works online only. Its transport and its screens are Visual Builder’s (its own client, action chains and page variables, not the shared web client nor the React Native core), so the offline session, the local answers and the Sync UI would have to be wired into that runtime on their own. Use Vaadin or a React renderer for a UI that must work offline in the browser.
  • Same scope as the phone: AutoCrud, capability listings and YAML pages over REST sources. Custom Java actions need a connection.
  • A page reloaded offline at a route that was never opened online opens the app shell at that route; a crud’s first load (its mediator) is kept per crud the first time it is opened online, so open each offline screen once while online.
  • The leader tab syncs. If it is closed, another tab takes over within a few seconds.
  • Safari’s private windows give no persistent storage: the copy lasts for the window.