Skip to content
SEUNGWOO CHOI edited this page Nov 3, 2026 · 1 revision

PWA, offline use, and updates

English · 한국어

The production static export uses a Service Worker for public application resources. Local journal data remains in the encrypted vault; installing the shell does not sign in, unlock a vault, or synchronize records. See architecture, self-hosting, and backup and recovery.

Installation and cache boundaries

Run the full npm run build in web/. After Next.js and CSP processing, scripts/finalize-sw.mjs writes a deterministic version/build-hash cache name and sorted BUILD_ASSETS list into out/sw.js. The list includes all emitted /_next/static/ JS/CSS, including lazy Account, backup, diagnostics, and Worker chunks. npm run check:sw checks both the cache name and precache list against the export.

Installation uses one cache.addAll for /, /manifest.webmanifest, /theme-init.js, and the executable list. Unhashed shell requests use cache: "reload"; content-addressed chunks use cache: "force-cache". Both omit credentials. A failed required fetch rejects installation; skipWaiting() runs only after the complete precache succeeds. Wait for an installed, activated controller before relying on first-use offline features. The first interactive paint alone does not establish offline readiness.

Request Strategy
Same-origin GET navigation to / Network first with HTTP-cache revalidation (cache: "no-cache"); network failure or non-success HTTP status falls back to the installed build's /
/manifest.webmanifest, /theme-init.js Network first with the same installed-build fallback
Same-origin /_next/static/ JS/CSS Current app cache, then retained app caches, then network; a network miss is not added to the cache
Non-navigation GET to /, including queries and alternate-representation headers Browser/network handling; never substitute cached shell HTML
API, sessions, blobs, cross-origin, non-GET, and other paths/assets Browser/network handling; no Service Worker cache

Online HTML never overwrites the installed build's cached HTML. A newer online document can therefore load while the previous complete build remains the offline fallback until the new installation succeeds. This depends on consistent hosting of complete outputs.

The executable list excludes source maps, media, and separate WASM files. It is not a general offline cache for every asset or route. A non-navigation root fetch can fail offline while navigation to / still works. The app bypasses Service Worker registration on localhost and 127.0.0.1; the test suites use a separate loopback origin to exercise production registration.

Existing tabs and cache cleanup

Activation claims clients without forcing a document reload. An older open document keeps its editor and draft and can load its original lazy chunks from retained app caches after a newer worker takes control. Only caches prefixed miroven-shell- or legacy private-journal-shell- are consulted or cleaned; unrelated caches are left alone.

The worker snapshots obsolete app cache names at activation. It removes them when no window could still need them: at activation with no open windows, or after a successful online root navigation with no other window apart from the navigating/replaced client. Multiple open windows can defer cleanup even if they all use the latest build. A failed/offline navigation does not trigger this cleanup. A newer worker's installation cache created after that snapshot is preserved. If the worker process restarts and loses its in-memory list, cleanup can wait until a later activation.

This update behavior does not change PIN key lifetime. A controlled old document still runs its existing JavaScript; follow the security model when a security update requires closing/reloading old tabs.

Cached photo Workers

For cached worker and sharedworker script requests, the Service Worker returns a fresh Response with the cached body, status, and headers, without the original response URL. This preserves the requested URL's fragment, including Turbopack's #params bootstrap data used by Safari. CSP and MIME headers remain intact. Ordinary JS/CSS responses keep their normal cached response. See image processing.

Verification

Run from web/, with installed dependencies, OpenSSL, and Playwright Chromium/WebKit. Keep ports 3101/3102 free for the HTTPS test server:

npx playwright install chromium webkit
npm run build
npm run test:offline
npm run test:updates

test:offline uses playwright.offline.config.ts with a fresh server and the existing out/. App journeys use https://127.0.0.2:3101; tests/offline/updates.spec.ts serves small synthetic A/B shells on an isolated loopback port with the actual Service Worker source. Chromium covers first/repeat offline writing, persisted entries/drafts, unopened Account, backup import/export, diagnostics, executable precaches, online cache reuse, incomplete B installation with A fallback, B activation, old-tab lazy import, and cache cleanup. The cached photo Worker case also runs in iPhone-emulated WebKit, forces the Canvas fallback, and verifies actual WebP WASM encoding after Service Worker activation and reload. It runs online and does not establish offline WASM availability or physical-device compatibility.

test:updates uses playwright.updates.config.ts and tests/updates/build-fixtures.mjs. It compiles an isolated source copy twice through the normal Next.js build and CSP/SW finalizers. Only the fixture's lazy Account component receives an A/B attribute, which must produce distinct chunk URLs and build caches. Fixtures are local-only, share installed dependencies, exclude .env files, and leave the checkout's source, .next/, and out/ intact. Outputs remain in ignored web/.local/update-builds/; ESLint excludes .local/ artifacts.

The Chromium test switches A→B on the same disposable HTTPS origin and waits for controller change and the activated state. The server then serves only B, so A's Account chunk returns 404 online. After clearing HTTP cache and going offline, it opens the previously unopened A Account, checks the actual A chunk came from the Service Worker, and exports an encrypted backup. It verifies the original document token and draft, complete B precache, and B Account after offline reload. Unit tests separately cover boundary filtering, manifest integrity, installation failure, retained caches, cleanup races, and Worker response URLs.

Both functional suites are included in npm run verify:full, and therefore its CI job. These are controlled compilations of the current app and synthetic shell transitions, not historical-release or mixed-CDN compatibility tests. Performance benchmarks are separate; see client performance. Test HTTPS exceptions apply to disposable test browsers, not production TLS policy.

Source: public/sw.js, finalize-sw.mjs, tests/offline, and tests/updates.

Clone this wiki locally