Installable app and offline browsing
Riviamigo ships as an installable progressive web app (PWA). This page covers how it is built and the rules that keep it safe to change. The user-facing guide is Install Riviamigo as an app.
Pieces
| Piece | Location | Role |
|---|---|---|
| Web app manifest | apps/web/public/manifest.webmanifest | Name, colors, icons, shortcuts, standalone display. |
| Icons | apps/web/public/pwa/ | 192/512 any icons, 192/512 maskable icons, 180 px Apple touch icon. Rendered from docs/assets/readme/logo-lockup-dark.png. |
| Head metadata | apps/web/index.html | Manifest link, Apple standalone tags, viewport-fit=cover, fallback theme-color. |
| Service worker source | apps/web/sw/service-worker.ts | Install, activate, and fetch handling. Type-checked with apps/web/sw/tsconfig.json (WebWorker lib). |
| Routing rules | apps/web/src/pwa/serviceWorkerRouting.ts | Pure request classification and shell validation, shared by the worker and its unit tests. |
| Build plugin | apps/web/tooling/pwaServiceWorkerPlugin.ts | Bundles the worker with esbuild and injects this build's precache manifest as dist/sw.js. Runs only for vite build. |
| Registration and updates | apps/web/src/pwa/serviceWorkerRegistration.ts | Registers /sw.js in production, detects a waiting build, applies it on request. |
| Install prompt | apps/web/src/pwa/installPrompt.ts, apps/web/src/features/settings/InstallAppCard.tsx | Captures beforeinstallprompt; Settings → Appearance offers install or platform instructions. |
| Theme color | apps/web/src/pwa/themeColor.ts | Keeps <meta name="theme-color"> equal to the active --rm-bg-surface token. |
| Offline session | packages/hooks/src/serverReachability.ts, packages/hooks/src/useAuth.ts, apps/web/src/components/layout/AuthGuard.tsx | Tells "signed out" apart from "server unreachable" and shows saved data read-only. |
| Status banner | apps/web/src/components/layout/AppStatusBanner.tsx | Offline and update-ready notices above page content. |
| Edge config | compose/nginx/nginx.conf | no-cache for sw.js and the manifest, manifest media type, worker-src/manifest-src CSP. |
Service worker policy
The worker serves only the static application shell and build output. It never caches or answers API traffic.
- Never intercepted: non-GET requests, cross-origin requests,
/v1/*,/v2/*,/health, websockets, and/sw.jsitself. These include the single-use OIDC callback, so navigation preload stays disabled: it would issue a second request for navigations the worker bypasses. - Navigations: network first. The precached
index.htmlis used only when the network request throws. A reachable server's response, including an identity-aware proxy's sign-in redirect, always reaches the page. /assets/*(content-hashed): cache first from the precache, then the previous build's assets (riviamigo-previous-assets), then the runtime cache, then the network.- Public icons, brand art, packaged vehicle fallbacks: served from cache and revalidated in the background.
- What may be cached at runtime: only a same-origin, unredirected
200of the expected media type (JS, CSS, or font for/assets/*; an image or JSON manifest for public files). A proxy's sign-in page or the SPA's HTML fallback for a missing file is passed through but never stored.
Precache and versioning
The build plugin precaches every emitted JS, CSS, and font file plus the public
SVGs, manifest, and pwa/ icons. Source maps and vehicle fixture images are
excluded. The manifest version is a hash of the precached file names, public
file contents, and index.html, so any deploy that changes the shell produces a
new sw.js.
On install the worker fetches /index.html with cache: 'reload' and stores it
only when it is a same-origin, unredirected 200 text/html response that
references this build's entry chunk. A proxy sign-in page or error page aborts
the install; the browser retries on a later visit.
Activation copies the outgoing build's /assets/* entries into
riviamigo-previous-assets (replacing whatever an earlier activation put
there, so it holds one older build at most), deletes the older
riviamigo-precache-* caches, and empties the runtime cache.
Updates
A new worker installs in the background and waits. When one is waiting and the
page already has a controller, the status banner offers Reload. Accepting
posts riviamigo:skip-waiting; the new worker activates and only the tab that
asked reloads. Other open tabs are now controlled by the new worker but keep
running their loaded build: their lazy chunks come from
riviamigo-previous-assets, so they keep working even after the deploy removed
those files from the server, and unsaved edits there are not discarded. Their
banner keeps offering Reload, which then simply reloads. A tab whose first
visit installs the worker is not told about an update. Open tabs check for a
newer worker hourly and whenever they become visible.
Turning the worker off
A build made with VITE_PWA_DISABLED=true (for the image, docker build --build-arg VITE_PWA_DISABLED=true against compose/Dockerfile) ships
sw/kill-switch-worker.ts as sw.js. Browsers fetch it on their next update
check, even from a tab whose current worker is still serving a cached shell;
it takes over at once, deletes every riviamigo-* cache, and unregisters
itself. The page also unregisters workers and deletes those caches when it
loads. Use it only as a release-level escape hatch; because navigations are
network first, a normal deploy already replaces the shell for every online
client.
Offline browsing
The query cache already persists selected query roots to localStorage per
user for up to 24 hours (apps/web/src/queryClient.ts). Offline browsing reuses
that cache; it does not add a second store.
- The transport reports every response and connection failure to
useServerReachability:- A request that never reaches the server is
NETWORK_ERROR. - 502, 503, 520–523, or 530 without the API's JSON error envelope
(
{"error": {"code": ...}}) isSERVER_UNAVAILABLE: the gateway answered, the API did not. 530 is Cloudflare's code for a down tunnel. - 504 or 524 without the envelope is
GATEWAY_TIMEOUT. One slow request does not mean an outage, so it leaves reachability unchanged. - The API's own 502/503/504 responses, such as an unavailable Rivian upstream, keep their API code and count as reachable.
- Caller-cancelled requests are ignored.
- A request that never reaches the server is
resumeSessionOutcome()returnsresumed,signed-out, orunreachable(the bootstrap is cheap, so a gateway timeout there counts as unreachable).resumeSession()remains the boolean wrapper.- On bootstrap, an
unreachableoutcome with a persisted user id and a fresh saved cache switches to offline browsing inside the same store update that ends bootstrapping (allowOffline), so the guard never observes a signed-out moment. There is no access token in this state, so everyuseAuthReady-gated query stays idle and renders only cached data. Asigned-outoutcome, or no saved cache, goes to sign-in as before. startReconnectProbe()(apps/web/src/components/layout/reconnectProbe.ts) runs for as long as the guard is mounted. While the server is unreachable or the app is browsing offline, it probesGET /v1/auth/configwith backoff (5 s up to 60 s), immediately on the browseronlineevent, when the tab becomes visible, and when the banner's Retry now is pressed. It reads the stores directly rather than re-running on their changes, so the reachability flip caused by a successful probe cannot cancel the reconnect that follows. When the server answers, an offline session resumes for real or ends at sign-in, and an online session refetches queries that failed during the outage.- The probe bypasses the client rate-limit cooldown, times out after 10 s, and does not follow redirects. A redirect, 401, or 403 from this public endpoint means a gateway in front of Riviamigo wants the user to sign in again, which only a full page load can show. A live session may hold unsaved edits, so the status banner asks the user to reload. Read-only offline browsing reloads by itself, at most once every two minutes per tab so a gateway that rejects background requests cannot cause a loop. If the server answers the probe but the session still cannot resume, the probe keeps backing off rather than retrying every five seconds.
- The cache is saved only for the in-memory signed-in account, with an access token, outside offline browsing, and while the server is reachable. Offline use therefore never extends the 24-hour limit, and another tab's sign-in cannot redirect one account's data to another's key. Offline browsing ends, and the saved data is removed, when that data reaches its 24-hour limit.
Offline browsing persists the account context the shell needs (me, theme,
time zone, unit preferences) alongside the existing history roots. Full-density
metric series are not persisted, so charts that were not cached show their
normal loading state until the connection returns.
Security notes
- Offline mode never fabricates authentication. It shows data that was already stored on the device for that user, under the same 24-hour, per-user rules as before, and clears on sign-out.
- An explicit sign-out removes the persisted user id, so offline mode cannot start for a signed-out browser.
- Signing out clears the device at once and records a pending sign-out
(
rm-pending-logout) before callingPOST /v1/auth/logout, which revokes the session from the refresh cookie alone. If that request fails, for example while offline, the next session resume calls it again instead of resuming, and a new password, invitation, or SSO sign-in settles it first. Settling keeps the marker when the server still cannot be reached, so a sign-in attempted offline cannot erase it. While it is pending, no tab refreshes its access token, so another open tab cannot keep the session alive. A session the user signed out of is therefore never silently resumed. - A different account resuming after offline browsing clears the previous account's in-memory data in a layout effect, and the guard renders nothing while the hydrated account differs from the session's, so one account's data is never shown under another's session.
Mobile standalone layout
viewport-fit=cover makes env(safe-area-inset-*) meaningful. The shared mobile
header in packages/ui/src/primitives/Sidebar.tsx adds the top inset to its
height and padding and the side insets to its horizontal padding;
AppLayout's content wrapper offsets by the same amount and pads for the
bottom and side insets. iOS uses apple-mobile-web-app-status-bar-style: default, so the status bar follows theme-color instead of drawing white
text over a light header.
Verification
- Unit tests:
apps/web/src/pwa/__tests__/,apps/web/src/test/serverReachability.test.ts,apps/web/src/test/reconnectProbe.test.ts,apps/web/src/test/queryCachePersistence.test.ts,apps/web/src/test/appStatusBanner.test.tsx, and the offline cases inapps/web/src/test/authGuard.test.tsx. pnpm --filter @riviamigo/web buildprints the precache summary ([pwa] sw.js <version>: <n> precached files).- Manual: serve a production build (
vite preview), check the Application panel for the manifest and an activated worker, then stop the API and reload to see the offline banner over saved data.
Not included
Push notifications need a server-side alert system that does not exist yet. They are tracked on the roadmap.