mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-12 22:26:13 +00:00
fix(pwa): keep the static cache across sign-in and sign-out
The session wipe deleted every HealthLog cache, the static one too, although it holds only locale catalogs and icons. Each sign-in and sign-out threw away the 190 KB catalog and refetched it. The wipe now deletes the page cache and the API caches older releases left behind, current and legacy versions, and keeps the static cache. Function name and signature are unchanged.
This commit is contained in:
1 parent
433b92a67f
commit
960f3c4878
3 files changed
+21
-14
No files matched your search
@@ -98,7 +98,7 @@ describe("session-end CacheStorage cleanup", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("deletes current and legacy HealthLog static/page/data caches only", async () => {
|
||||
it("deletes current and legacy HealthLog page and data caches only", async () => {
|
||||
const cacheNames = [
|
||||
"healthlog-static-v1.34.0",
|
||||
"healthlog-static-v1.15.20",
|
||||
@@ -124,9 +124,11 @@ describe("session-end CacheStorage cleanup", () => {
|
||||
.soft(deleted.sort())
|
||||
.toEqual(
|
||||
cacheNames
|
||||
.filter((name) => /^healthlog-(?:static|pages|data)-/.test(name))
|
||||
.filter((name) => /^healthlog-(?:pages|data)-/.test(name))
|
||||
.sort(),
|
||||
);
|
||||
// The static cache holds catalogs and icons, nothing of an account.
|
||||
expect(deleted).not.toContain("healthlog-static-v1.34.0");
|
||||
expect(deleted).not.toContain("unrelated-app-static-v7");
|
||||
expect(deleted).not.toContain("workbox-precache-healthlog");
|
||||
});
|
||||
|
||||
@@ -36,10 +36,7 @@ import {
|
||||
import { trackBackgroundTask } from "@/lib/logging/background-tasks";
|
||||
import { queryKeys } from "@/lib/query-keys";
|
||||
import { isReadingSharedRecord } from "@/lib/query-keys/record-scope";
|
||||
// Session-end cleanup removes every current/legacy HealthLog cache family
|
||||
// while leaving unrelated CacheStorage namespaces on the same origin
|
||||
// untouched; the name contract is shared with `public/sw.js`.
|
||||
import { HEALTHLOG_CACHE_NAME_RE } from "@/lib/pwa/sw-cache-policy";
|
||||
import { ACCOUNT_CACHE_NAME_RE } from "@/lib/pwa/sw-cache-policy";
|
||||
|
||||
const DB_NAME = "healthlog-query-cache";
|
||||
const STORE = "kv";
|
||||
@@ -335,9 +332,11 @@ export async function clearPersistedQueryCache(): Promise<void> {
|
||||
|
||||
/**
|
||||
* Wipe every client-side cache that can hold one account's data: the
|
||||
* IndexedDB query snapshot and the worker's caches, including the API caches
|
||||
* older releases left behind. The single call for a session END (logout or
|
||||
* 401 / expiry redirect). Best-effort across the board.
|
||||
* IndexedDB query snapshot, the worker's page cache and the API caches older
|
||||
* releases left behind, current and legacy versions alike. The static cache
|
||||
* (catalogs, icons) holds nothing of an account and stays, so a sign-in does
|
||||
* not download the locale catalog again. The single call for a session END
|
||||
* (logout or 401 / expiry redirect) and at sign-in. Best-effort.
|
||||
*/
|
||||
export async function clearOfflineCachesForSessionEnd(): Promise<void> {
|
||||
await clearPersistedQueryCache();
|
||||
@@ -346,7 +345,7 @@ export async function clearOfflineCachesForSessionEnd(): Promise<void> {
|
||||
const keys = await caches.keys();
|
||||
await Promise.all(
|
||||
keys
|
||||
.filter((k) => HEALTHLOG_CACHE_NAME_RE.test(k))
|
||||
.filter((k) => ACCOUNT_CACHE_NAME_RE.test(k))
|
||||
.map((k) => caches.delete(k)),
|
||||
);
|
||||
} catch {
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
/**
|
||||
* The worker's own cache names: `healthlog-{static,pages,data}-<version>`.
|
||||
* Everything the app deletes from CacheStorage (session end, the "new
|
||||
* version" reload) matches against this and nothing else, so another app's
|
||||
* caches on the same origin are left alone. `data` names the API cache older
|
||||
* releases kept; it stays so those caches are still deleted. Same pattern as
|
||||
* Everything the app deletes from CacheStorage matches one of the two
|
||||
* patterns below and nothing else, so another app's caches on the same
|
||||
* origin are left alone. `data` names the API cache older releases kept; it
|
||||
* stays so those caches are still deleted. Same pattern as
|
||||
* `HEALTHLOG_CACHE_NAME_RE` in `public/sw.js`.
|
||||
*/
|
||||
export const HEALTHLOG_CACHE_NAME_RE = /^healthlog-(?:static|pages|data)-/;
|
||||
|
||||
/**
|
||||
* The caches that can hold something of one account: cached pages and the
|
||||
* old API answers. The session wipe deletes these and keeps the static cache.
|
||||
*/
|
||||
export const ACCOUNT_CACHE_NAME_RE = /^healthlog-(?:pages|data)-/;
|
||||
Reference in new issue
Block a user