fix(pwa): re-anchor the sw.js fallback version on every build

`public/sw.js` carries a `|| "..."` fallback literal used for `CACHE_VERSION`
when `importScripts('/sw-version.js')` is unavailable. The literal had drifted
to `v1.4.43`, four-plus minors behind `package.json`; in any environment where
the generated file is missing this keyed every cache to the stale version and
turned the activate-step eviction into a no-op — the exact bug the generator
was written to kill.

Extend the prebuild generator to rewrite the fallback literal in `sw.js` to the
current `package.json` version on every build, anchored on a
`/* @sw-version-fallback */` marker (the build fails loudly if the marker is
gone). The literal and the generated `sw-version.js` can no longer diverge.
The SW test now reads the fallback straight from the source so it tracks the
literal instead of hard-coding it.
This commit is contained in:
Marc-André Bombeck committed 2026-06-14 10:47:50 +02:00
1 parent 083410473c
commit 60f587c5a7
3 files changed
+39 -6

No files matched your search

+10 -4
View File
@@ -24,10 +24,16 @@ const SW_SOURCE = readFileSync(
);
const ORIGIN = "https://app.example";
// `importScripts` throws in the sandbox, so the literal fallback version
// is active and the cache names are deterministic.
const CURRENT_PAGE_CACHE = "healthlog-pages-v1.4.43";
const CURRENT_STATIC_CACHE = "healthlog-static-v1.4.43";
// `importScripts` throws in the sandbox, so the in-`sw.js` fallback literal
// is active and the cache names are deterministic. The fallback is
// re-anchored to `package.json` on every prebuild by
// `scripts/generate-sw-version.mjs`; read it straight from the SW source so
// this test never drifts from the literal it is exercising.
const FALLBACK_VERSION = /\/\* @sw-version-fallback \*\/\s*"(v[^"]*)"/.exec(
SW_SOURCE,
)![1];
const CURRENT_PAGE_CACHE = `healthlog-pages-${FALLBACK_VERSION}`;
const CURRENT_STATIC_CACHE = `healthlog-static-${FALLBACK_VERSION}`;
class FakeCache {
map = new Map<string, Response>();