mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
fix(pwa): serve allowlisted API reads network-first
The offline service worker cached allowlisted GET /api/* reads with stale-while-revalidate, serving the cached pre-mutation copy first and revalidating in the background. Online, a list fetched straight after a create or update rendered the old (or empty) data until the next paint, breaking read-after-write. Switch the data branch to network-first: always fetch fresh from the network online and refresh the cache behind it; fall back to the last cached copy only when the network fails (offline). The offline fallback, the allowlist plus auth/token/secret-body boundary, and the release-versioned cache name are unchanged. Add a regression guard to the cache-policy test that reads public/sw.js and asserts the network-first wiring and the absence of the old stale-while-revalidate handler.
This commit is contained in:
1 parent
36ce82866a
commit
730430728d
2 files changed
+88
-47
No files matched your search
@@ -1,3 +1,6 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
@@ -5,6 +8,11 @@ import {
|
||||
isCacheableApiResponse,
|
||||
} from "@/lib/pwa/sw-cache-policy";
|
||||
|
||||
const SW_SOURCE = readFileSync(
|
||||
join(process.cwd(), "public", "sw.js"),
|
||||
"utf8",
|
||||
);
|
||||
|
||||
describe("isAllowlistedApiRead — offline read boundary", () => {
|
||||
it("caches the curated safe GET read endpoints", () => {
|
||||
expect(isAllowlistedApiRead("/api/dashboard/snapshot")).toBe(true);
|
||||
@@ -91,3 +99,32 @@ describe("isCacheableApiResponse — body/header refusal", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// v1.18.6.x regression guard. The first v1.18.6 cut cached allowlisted API
|
||||
// reads with stale-while-revalidate, which served the pre-mutation copy first
|
||||
// and broke read-after-write (a list fetched straight after a create showed
|
||||
// the old/empty data). The strategy must be network-first: always fetch fresh
|
||||
// online, fall back to cache ONLY when the network throws (offline).
|
||||
describe("public/sw.js — allowlisted API reads are network-first", () => {
|
||||
it("routes the allowlisted API branch through a network-first handler", () => {
|
||||
expect(SW_SOURCE).toContain("networkFirstApi(request)");
|
||||
expect(SW_SOURCE).toContain("async function networkFirstApi(request)");
|
||||
});
|
||||
|
||||
it("no longer uses stale-while-revalidate for API reads", () => {
|
||||
expect(SW_SOURCE).not.toContain("staleWhileRevalidateApi");
|
||||
});
|
||||
|
||||
it("falls back to the cached copy only inside the network-failure catch", () => {
|
||||
const handler = SW_SOURCE.slice(
|
||||
SW_SOURCE.indexOf("async function networkFirstApi(request)"),
|
||||
);
|
||||
const tryIdx = handler.indexOf("await fetch(request)");
|
||||
const catchIdx = handler.indexOf("} catch {");
|
||||
const cacheMatchIdx = handler.indexOf("cache.match(request)");
|
||||
// fetch comes first; the cache read lives after the catch boundary.
|
||||
expect(tryIdx).toBeGreaterThan(-1);
|
||||
expect(catchIdx).toBeGreaterThan(tryIdx);
|
||||
expect(cacheMatchIdx).toBeGreaterThan(catchIdx);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user