Files
valknar 5423ca70f4 Fix stale/mismatched engine cache causing "ASM_CONSTS[...] is not a function" live
The service worker's engine cache key relied on a manual CACHE_VERSION
bump that was forgotten across the last two @valknar/vpinball-wasm
version bumps, so returning visitors kept getting served old cached
vpinball.js/.wasm/.data via cache-first with no revalidation — the origin
server itself was serving a correct, matching 0.3.4 set the whole time
(verified via headers/sizes against the local build).

Split CACHE_VERSION into a manually-bumped SHELL_VERSION and an
ENGINE_VERSION that copy-engine-assets.mjs now writes automatically from
the installed engine's package.json on every build, so the engine cache
always busts in lockstep with the dependency version — no step to forget.
2026-08-24 14:42:38 +02:00

84 lines
2.6 KiB
JavaScript

// Bump manually (via `pnpm sw:bump`) whenever SHELL_ASSETS or the shell's
// caching behavior changes — nothing else invalidates the shell cache, since
// none of the cached URLs below are content-hashed by us.
const SHELL_VERSION = "v2";
// Written automatically by scripts/copy-engine-assets.mjs from the installed
// @valknar/vpinball-wasm version — do not edit by hand. Ties the engine
// cache key to the actual engine build, so a version bump always busts
// stale caches instead of relying on a human to remember a separate step
// (which is exactly how returning visitors ended up on a stale, and
// possibly internally inconsistent, engine bundle before this existed).
const ENGINE_VERSION = "0.3.4";
const SHELL_CACHE = `shell-${SHELL_VERSION}`;
const ENGINE_CACHE = `engine-${ENGINE_VERSION}`;
const SHELL_ASSETS = [
"/",
"/play/",
"/manifest.webmanifest",
"/icons/icon-192.png",
"/icons/icon-512.png",
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches
.open(SHELL_CACHE)
.then((cache) => cache.addAll(SHELL_ASSETS))
.then(() => self.skipWaiting()),
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(
keys
.filter((key) => key !== SHELL_CACHE && key !== ENGINE_CACHE)
.map((key) => caches.delete(key)),
),
)
.then(() => self.clients.claim()),
);
});
self.addEventListener("fetch", (event) => {
const url = new URL(event.request.url);
if (event.request.method !== "GET" || url.origin !== self.location.origin) return;
// The engine's .wasm/.data/.js are large and effectively immutable per
// release (they aren't content-hashed), so once fetched they're served
// straight from cache — cache-busting happens by bumping CACHE_VERSION.
if (url.pathname.startsWith("/vendor/vpinball-wasm/")) {
event.respondWith(
caches.open(ENGINE_CACHE).then((cache) =>
cache.match(event.request).then(
(hit) =>
hit ??
fetch(event.request).then((response) => {
cache.put(event.request, response.clone());
return response;
}),
),
),
);
return;
}
event.respondWith(
caches.match(event.request).then(
(hit) =>
hit ??
fetch(event.request).then((response) => {
if (response.ok) {
const clone = response.clone();
caches.open(SHELL_CACHE).then((cache) => cache.put(event.request, clone));
}
return response;
}),
),
);
});