Files
roast_command_center/public/sw.js
T
snowspeeder 59645e59b5 Add green-bean inventory and cupping features
Ports inventory management and SCA-style cupping scoring from
hope_roaster: lot tracking with audit-logged consumption, cupping
sessions with server-authoritative scoring and a live radar chart,
and links from the planner (draw-from-lot, open-cupping-session).

Also fixes the Blend/Single-origin toggle layout, replaces tooltips
with an in-context "why" teaching layer, and stages the planner UI
into pre-roast vs. post-roast phases.
2026-07-30 14:47:15 -04:00

98 lines
2.5 KiB
JavaScript

const CACHE_NAME = "roast-planner-static-v6";
// Only /app is precached as a navigable page: it is a data-free authenticated shell (draft
// data lives only in account-scoped local storage, and logout clears that namespace), so it is
// the one page that is both safe and useful to relaunch offline. The marketing page, auth
// pages, account, admin, inventory, and cupping all depend on the network (auth checks, live
// data) and are deliberately left off this list rather than cached in a way that could show
// stale content.
const APP_SHELL = [
"/app",
"/app.css",
"/worksheet.css",
"/manifest.webmanifest",
"/icon.svg",
"/js/main.js",
"/js/api.js",
"/js/nav.js",
"/js/toast.js",
"/js/why-panels.js",
"/js/lot-picker.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",
"/shared/cupping.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") {
// Only /app has a meaningful offline shell (see APP_SHELL comment above); every other
// page requires the network anyway, so let those navigations fail normally when offline
// rather than risk serving stale auth/account/admin content.
if (url.pathname !== "/app") return;
event.respondWith(
fetch(request).catch(
async () => (await caches.match("/app")) || Response.error(),
),
);
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();
});