Files
tavolo/web/sw.js
T
woggioni 8c6ad10bf6 Make the frontend an installable PWA
Add a web app manifest and an install-only service worker (no offline
caching: live state always comes from the server and hashed asset names
change every build), registered from the WASM bundle. The worker must
live at the site root for its scope to cover the app, and Granian only
serves /static and /assets, so tavolo/static.py answers
/manifest.webmanifest and /sw.js from STATIC_DIR with the content types
browsers require; index.html gains the manifest link, icons, theme-color
and Apple standalone meta tags.

Icons are committed under web/assets/icons/ with vector sources and a
make-icons.sh regeneration script, mirroring the card art workflow.
2026-10-14 22:14:43 +00:00

35 lines
1.3 KiB
JavaScript

// Tavolo service worker: installability only, no offline caching.
//
// The app is a thin client for a live game server: state arrives over
// /api and /ws and is never safe to replay from a cache, and a new build
// changes the hashed asset names, so stale cached files would break the
// app until the cache expired. The worker therefore only performs the
// passthrough fetch below — enough for the browser to treat the app as
// installable — and leaves every request's caching to the network stack.
self.addEventListener("install", () => {
// A new worker takes over on the next reload instead of waiting for
// every tab of the old version to close.
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(self.clients.claim());
});
self.addEventListener("fetch", (event) => {
const url = new URL(event.request.url);
// Let the browser handle other origins and the live endpoints directly
// (respondWith would only add a hop; /auth navigations must be allowed
// to follow their redirects untouched).
if (
url.origin !== self.location.origin ||
url.pathname.startsWith("/api/") ||
url.pathname.startsWith("/auth/") ||
url.pathname.startsWith("/ws")
) {
return;
}
event.respondWith(fetch(event.request));
});