taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 3da28b34d42a7bfdd3c01301bdc4b408ab328932
parent 98b5ff80eec01f9adf71cd828129fd8f06f735d9
Author: Florian Dold <dold@taler.net>
Date:   Thu, 26 Nov 2026 04:13:04 +0100

wallet-webui: let the wallet open offline

The service worker cached the scripts and styles, but an offline reload
could still fail to load the HTML. Cache the entry page and use it when
navigation to the wallet fails, including when the URL carries a payment
link.

Diffstat:
Mpackages/wallet-webui/build.mjs | 24++++++++++++------------
Mpackages/wallet-webui/src/platform/service-worker.ts | 20++++++++++++++++++++
Mpackages/wallet-webui/test/pwa-lifecycle.test.ts | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 97 insertions(+), 12 deletions(-)

diff --git a/packages/wallet-webui/build.mjs b/packages/wallet-webui/build.mjs @@ -220,24 +220,22 @@ const pwaVersion = await contentVersion(pwa, [ ...fontNames.map((name) => `font/${name}`), ...(sqliteEnabled ? ["sqlite3.wasm"] : []), ]); +const pwaShellAssets = { + app: `./app.js?v=${pwaVersion}`, + styles: `./styles.css?v=${pwaVersion}`, + manifest: `./manifest.webmanifest?v=${pwaVersion}`, + icon: `./taler-logo-light.svg?v=${pwaVersion}`, +}; const versionedPwaAssets = [ - `./app.js?v=${pwaVersion}`, + ...Object.values(pwaShellAssets), `./wallet-worker.js?v=${workerVersion}`, - `./styles.css?v=${pwaVersion}`, - `./manifest.webmanifest?v=${pwaVersion}`, backupReviewUrl, - `./taler-logo-light.svg?v=${pwaVersion}`, `./taler-logo-dark.svg?v=${pwaVersion}`, `./qr-logo.svg?v=${pwaVersion}`, ...fontNames.map((name) => `./font/${name}`), ...(sqliteEnabled ? [`./sqlite3.wasm?v=${sqliteWasmVersion}`] : []), ]; -await html(pwa, "index.html", "GNU Taler Wallet", { - app: versionedPwaAssets[0], - styles: versionedPwaAssets[2], - manifest: versionedPwaAssets[3], - icon: versionedPwaAssets[4], -}); +await html(pwa, "index.html", "GNU Taler Wallet", pwaShellAssets); const demo = new URL("demo/", dist); await mkdir(demo, { recursive: true }); @@ -284,8 +282,10 @@ await html(demo, "index.html", "GNU Taler Wallet Demo", { await build({ ...shared, define: defineBuildConstants({ - __STATIC_CACHE_VERSION__: JSON.stringify(pwaVersion), - __STATIC_ASSETS__: JSON.stringify(versionedPwaAssets), + __STATIC_CACHE_VERSION__: JSON.stringify( + await contentVersion(pwa, ["index.html"]), + ), + __STATIC_ASSETS__: JSON.stringify(["./index.html", ...versionedPwaAssets]), }), entryPoints: { "service-worker": "src/platform/service-worker.ts" }, outdir: pwa.pathname, diff --git a/packages/wallet-webui/src/platform/service-worker.ts b/packages/wallet-webui/src/platform/service-worker.ts @@ -76,6 +76,26 @@ self.addEventListener("message", (event) => { self.addEventListener("fetch", (event) => { if (event.request.method !== "GET") return; const requestUrl = new URL(event.request.url); + if (event.request.mode === "navigate") { + const scope = new URL(self.registration.scope); + const shell = new URL("index.html", scope); + if ( + requestUrl.origin !== scope.origin || + (requestUrl.pathname !== scope.pathname && + requestUrl.pathname !== shell.pathname) + ) + return; + event.respondWith( + fetch(event.request).catch(async (error) => { + const cachedShell = await ( + await caches.open(STATIC_CACHE) + ).match(shell.href); + if (!cachedShell) throw error; + return cachedShell; + }), + ); + return; + } if ( !STATIC_URLS.has(event.request.url) && (requestUrl.origin !== self.location.origin || diff --git a/packages/wallet-webui/test/pwa-lifecycle.test.ts b/packages/wallet-webui/test/pwa-lifecycle.test.ts @@ -45,3 +45,68 @@ test("wallet mutations revalidate without clearing the current screen data", () /await mutate\([\s\S]*?undefined,[\s\S]*?revalidate/, ); }); + +test("offline navigation returns the canonical cached shell only at wallet entry points", async (t) => { + const scope = "https://wallet.example/app/"; + type TestFetch = { + request: { url: string; method: string; mode: string }; + respondWith(response: Promise<Response>): void; + }; + let onFetch!: (event: TestFetch) => void; + const matches: string[] = []; + Object.assign(globalThis, { + __STATIC_CACHE_VERSION__: "test", + __STATIC_ASSETS__: ["./index.html", "./app.js?v=test"], + self: { + registration: { scope }, + location: new URL(scope), + addEventListener: (type: string, listener: typeof onFetch) => { + if (type === "fetch") onFetch = listener; + }, + }, + caches: { + open: async () => ({ + match: async (url: string) => { + matches.push(url); + return new Response("wallet shell"); + }, + }), + }, + }); + t.after(() => { + for (const property of [ + "self", + "caches", + "__STATIC_ASSETS__", + "__STATIC_CACHE_VERSION__", + ]) + Reflect.deleteProperty(globalThis, property); + }); + const network = t.mock.method( + globalThis, + "fetch", + async () => new Response("online"), + ); + await import("../src/platform/service-worker.js"); + function navigate(url: string, method = "GET") { + let result: Promise<Response> | undefined; + onFetch({ + request: { url, method, mode: "navigate" }, + respondWith: (response) => { + result = response; + }, + }); + return result; + } + assert.equal(await (await navigate(scope))?.text(), "online"); + network.mock.mockImplementation(async () => { + throw new Error("offline"); + }); + for (const url of [scope, scope + "index.html?private=test"]) { + assert.equal(await (await navigate(url))?.text(), "wallet shell"); + } + assert.deepEqual(matches, [scope + "index.html", scope + "index.html"]); + assert.equal(navigate(scope + "api"), undefined); + assert.equal(navigate("https://bank.example/app/index.html"), undefined); + assert.equal(navigate(scope, "POST"), undefined); +});