mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-10 13:22:14 +00:00
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:
1 parent
083410473c
commit
60f587c5a7
3 files changed
+39
-6
No files matched your search
@@ -23,6 +23,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const repoRoot = join(__dirname, "..");
|
||||
const pkgPath = join(repoRoot, "package.json");
|
||||
const outPath = join(repoRoot, "public", "sw-version.js");
|
||||
const swPath = join(repoRoot, "public", "sw.js");
|
||||
|
||||
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
||||
const version = `v${pkg.version}`;
|
||||
@@ -33,5 +34,23 @@ const body = `self.__APP_VERSION__ = ${JSON.stringify(version)};\n`;
|
||||
|
||||
writeFileSync(outPath, banner + body, "utf8");
|
||||
|
||||
// Also re-anchor the in-`sw.js` fallback literal to the current version, so
|
||||
// the literal used when `/sw-version.js` is missing (dev, or a 404 on the
|
||||
// generated file) can never drift stale — it bit v1.4.38.4 → v1.4.42. The
|
||||
// `/* @sw-version-fallback */` marker is the rewrite anchor; the literal that
|
||||
// follows it is replaced in place. If the marker is gone the SW source has
|
||||
// been restructured and the build must surface that rather than silently skip
|
||||
// the re-anchor.
|
||||
const sw = readFileSync(swPath, "utf8");
|
||||
const fallbackPattern = /(\/\* @sw-version-fallback \*\/\s*)"v[^"]*"/;
|
||||
if (!fallbackPattern.test(sw)) {
|
||||
process.stderr.write(
|
||||
"generate-sw-version: @sw-version-fallback marker not found in public/sw.js\n",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
const rewritten = sw.replace(fallbackPattern, `$1${JSON.stringify(version)}`);
|
||||
if (rewritten !== sw) writeFileSync(swPath, rewritten, "utf8");
|
||||
|
||||
// One-line log so the build output shows the version we baked in.
|
||||
process.stdout.write(`sw-version.js → ${version}\n`);
|
||||
process.stdout.write(`sw-version.js → ${version} (sw.js fallback re-anchored)\n`);
|
||||
Reference in new issue
Block a user