diff --git a/apps/web/src/lib/accountDeletionCleanup.test.ts b/apps/web/src/lib/accountDeletionCleanup.test.ts index 3719bc7cf..3b35b3111 100644 --- a/apps/web/src/lib/accountDeletionCleanup.test.ts +++ b/apps/web/src/lib/accountDeletionCleanup.test.ts @@ -4,7 +4,7 @@ import { describe, expect, it, vi } from "vitest"; import { clearPrivateDeviceData } from "./accountDeletionCleanup"; describe("clearPrivateDeviceData", () => { - it("clears query state, browser stores, caches, IndexedDB data, and offline packages", async () => { + it("clears query state, browser stores, private caches, IndexedDB data, and offline packages", async () => { localStorage.setItem("openmapx-private", "value"); sessionStorage.setItem("openmapx-session", "value"); const deletedCaches: string[] = []; @@ -34,11 +34,49 @@ describe("clearPrivateDeviceData", () => { expect(queryClient.clear).toHaveBeenCalledTimes(1); expect(localStorage.length).toBe(0); expect(sessionStorage.length).toBe(0); - expect(deletedCaches).toEqual(["app-shell-v1", "api-geodata"]); + expect(deletedCaches).toEqual(["api-geodata"]); expect(idbClears).toBe(1); expect(offlineClears).toBe(1); }); + it("keeps the worker's app-shell precache and deletes every runtime cache", async () => { + const runtimeCaches = [ + "style-assets-build-a", + "static-assets", + "maplibre-runtimes", + "api-geodata", + "api-category-search", + "api-autocomplete", + "api-weather", + "api-photos", + "map-tiles", + "street-level-imagery-tiles", + "vector-tiles", + "offline-package-glyphs-v1", + "openmapx-preferences", + "others", + "cross-origin", + "next-data", + ]; + const deletedCaches: string[] = []; + + const result = await clearPrivateDeviceData({ + queryClient: { clear: vi.fn() }, + cacheStorage: { + keys: async () => ["app-shell-build-a", "app-shell-build-b", ...runtimeCaches], + delete: async (name: string) => { + deletedCaches.push(name); + return true; + }, + }, + clearIdb: async () => {}, + clearOfflinePackages: async () => {}, + }); + + expect(deletedCaches).toEqual(runtimeCaches); + expect(result.failures).toBe(0); + }); + it("reports best-effort cleanup failures without misreporting server deletion as failed", async () => { const result = await clearPrivateDeviceData({ queryClient: { diff --git a/apps/web/src/lib/accountDeletionCleanup.ts b/apps/web/src/lib/accountDeletionCleanup.ts index c34cd79d9..28f3ed7d7 100644 --- a/apps/web/src/lib/accountDeletionCleanup.ts +++ b/apps/web/src/lib/accountDeletionCleanup.ts @@ -3,6 +3,7 @@ import type { QueryClient } from "@tanstack/react-query"; import { idbClear } from "./idbStore"; import { createOfflinePackageStorage } from "./offlineAreas"; +import { isAppShellCacheName } from "./swCaches"; interface CacheStorageCleanup { keys(): Promise; @@ -51,10 +52,19 @@ export async function clearPrivateDeviceData(options: AccountDeletionCleanupOpti (options.clearOfflinePackages ?? removeOfflinePackages)(), ]; if (cacheStorage) { + // Keep only the worker's public app-shell precache: it holds no personal + // data and is written only when a worker installs, so deleting it would + // leave offline navigations without the offline page until the next deploy. tasks.push( cacheStorage .keys() - .then((names) => Promise.all(names.map((name) => cacheStorage.delete(name)))), + .then((names) => + Promise.all( + names + .filter((name) => !isAppShellCacheName(name)) + .map((name) => cacheStorage.delete(name)), + ), + ), ); } const results = await Promise.allSettled(tasks); diff --git a/apps/web/src/lib/swCaches.ts b/apps/web/src/lib/swCaches.ts index eb18ee0b7..c501f05c7 100644 --- a/apps/web/src/lib/swCaches.ts +++ b/apps/web/src/lib/swCaches.ts @@ -53,6 +53,17 @@ export async function handleNetworkOnlyNavigation({ } } +/** + * Prefix of the worker's build-versioned precache of public shell files (the + * offline page, manifest and bundled styles), fetched without credentials and + * written only when a worker version installs. + */ +export const APP_SHELL_CACHE_PREFIX = "app-shell-"; + +export function isAppShellCacheName(name: string): boolean { + return name.startsWith(APP_SHELL_CACHE_PREFIX); +} + /** * Names of the app-shell precaches that currently exist. Client code cannot * name the build-versioned cache directly because the build id is defined only @@ -61,7 +72,7 @@ export async function handleNetworkOnlyNavigation({ */ export async function appShellCacheNames(): Promise { if (typeof caches === "undefined") return []; - return (await caches.keys()).filter((name) => name.startsWith("app-shell-")); + return (await caches.keys()).filter(isAppShellCacheName); } /** Return whether a build-versioned service-worker cache belongs to an older build. */ @@ -69,7 +80,7 @@ export function isStalePrecacheName( name: string, current: { appShell: string; style: string }, ): boolean { - if (name.startsWith("app-shell-")) return name !== current.appShell; + if (isAppShellCacheName(name)) return name !== current.appShell; if (name.startsWith("style-assets")) return name !== current.style; return false; } diff --git a/apps/web/src/sw.ts b/apps/web/src/sw.ts index 75e289ec9..f1f4dfa84 100644 --- a/apps/web/src/sw.ts +++ b/apps/web/src/sw.ts @@ -25,6 +25,7 @@ import { } from "serwist"; import { createNavigationRuntimeRoute } from "./lib/serviceWorkerNavigation"; import { + APP_SHELL_CACHE_PREFIX, isDirectionsApiPath, isMapLibreRuntimeAssetPath, isNetworkOnlyApiPath, @@ -53,7 +54,7 @@ declare const __SW_BUILD_ID__: string; declare const __MAPLIBRE_VERSION__: string; const OFFLINE_URL = STATIC_OFFLINE_FALLBACK_URL; -const APP_SHELL_CACHE = `app-shell-${__SW_BUILD_ID__}`; +const APP_SHELL_CACHE = `${APP_SHELL_CACHE_PREFIX}${__SW_BUILD_ID__}`; // Map-style assets (style JSON / sprite / TileJSON) are versioned by build id // too, so a deploy that changes the self-hosted style serves the new one on the // next load instead of the worker's stale copy. Old style caches are pruned on