Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions apps/web/src/lib/accountDeletionCleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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: {
Expand Down
12 changes: 11 additions & 1 deletion apps/web/src/lib/accountDeletionCleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>;
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 13 additions & 2 deletions apps/web/src/lib/swCaches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,15 +72,15 @@ export async function handleNetworkOnlyNavigation({
*/
export async function appShellCacheNames(): Promise<string[]> {
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. */
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;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from "serwist";
import { createNavigationRuntimeRoute } from "./lib/serviceWorkerNavigation";
import {
APP_SHELL_CACHE_PREFIX,
isDirectionsApiPath,
isMapLibreRuntimeAssetPath,
isNetworkOnlyApiPath,
Expand Down Expand Up @@ -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
Expand Down
Loading