e6658eae3d
Deploy / deploy (push) Successful in 29s
The worker isn't controlling the page on the first visit, so warm-up fetches bypassed it and no images were cached. The page now writes markers, its own JS/CSS/fonts and /set/ straight into the cache and only then shows 'Works offline'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
56 lines
1.7 KiB
JavaScript
56 lines
1.7 KiB
JavaScript
// Offline cache for /set (screen markers). Crews often have no signal on set:
|
|
// once the page has loaded once, it and every marker image work offline.
|
|
// Must match OFFLINE_CACHE in components/set/MarkerPicker.tsx
|
|
const CACHE = "ttvfx-set-v1";
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(caches.open(CACHE).then((c) => c.addAll(["/set/"])).then(() => self.skipWaiting()));
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
caches
|
|
.keys()
|
|
.then((keys) => Promise.all(keys.filter((k) => k.startsWith("ttvfx-set-") && k !== CACHE).map((k) => caches.delete(k))))
|
|
.then(() => self.clients.claim()),
|
|
);
|
|
});
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
const req = event.request;
|
|
if (req.method !== "GET") return;
|
|
const url = new URL(req.url);
|
|
if (url.origin !== self.location.origin) return;
|
|
|
|
// Page: network first so updates arrive, cached copy when offline
|
|
if (req.mode === "navigate") {
|
|
event.respondWith(
|
|
fetch(req)
|
|
.then((res) => {
|
|
const copy = res.clone();
|
|
caches.open(CACHE).then((c) => c.put("/set/", copy));
|
|
return res;
|
|
})
|
|
.catch(() => caches.match("/set/")),
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Marker images, build assets, fonts, icons: cache first
|
|
if (url.pathname.startsWith("/set/") || url.pathname.startsWith("/_next/") || /\.(png|woff2|webmanifest)$/.test(url.pathname)) {
|
|
event.respondWith(
|
|
caches.match(req).then(
|
|
(hit) =>
|
|
hit ||
|
|
fetch(req).then((res) => {
|
|
if (res.ok) {
|
|
const copy = res.clone();
|
|
caches.open(CACHE).then((c) => c.put(req, copy));
|
|
}
|
|
return res;
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
});
|