Files
roast_command_center/public/sw.js
T

87 lines
1.9 KiB
JavaScript

const CACHE_NAME = "roast-planner-static-v4";
const APP_SHELL = [
"/",
"/landing.html",
"/app",
"/app.css",
"/worksheet.css",
"/manifest.webmanifest",
"/icon.svg",
"/js/main.js",
"/js/landing.js",
"/js/admin.js",
"/js/prefill-ui.js",
"/js/alog-ui.js",
"/js/print.js",
"/shared/fields.js",
"/shared/ledger.js",
"/shared/time.js",
"/shared/reference-data.js",
"/shared/curve.js",
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)),
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(
keys
.filter((key) => key !== CACHE_NAME)
.map((key) => caches.delete(key)),
),
),
);
self.clients.claim();
});
self.addEventListener("fetch", (event) => {
const { request } = event;
let url;
try {
url = new URL(request.url);
} catch {
return;
}
if (
request.method !== "GET" ||
url.origin !== self.location.origin ||
url.pathname.startsWith("/api/")
)
return;
if (request.mode === "navigate") {
// /app is a data-free authenticated shell: draft data lives only in account-scoped
// local storage and logout clears that namespace. This makes offline relaunch useful
// without ever caching account data or API responses.
event.respondWith(fetch(request).catch(() =>
url.pathname === "/app" ? caches.match("/app") : caches.match("/landing.html"),
));
return;
}
// Prefer fresh JavaScript and styles so installed clients receive UI/security updates
// immediately; use the cache only when offline.
event.respondWith(
fetch(request)
.then((response) => {
if (response.ok)
caches
.open(CACHE_NAME)
.then((cache) => cache.put(request, response.clone()));
return response;
})
.catch(() => caches.match(request)),
);
});
self.addEventListener("message", (event) => {
if (event.data === "SKIP_WAITING") self.skipWaiting();
});