steelforesight/frontend/public/sw.js

30 lines
995 B
JavaScript

// ponytail: minimal runtime-cache SW. enough to make the app installable
// (Chrome needs a fetch handler) + offline shell. no precache list — Vite
// hashes bundle names at build, so we cache on first fetch instead.
const CACHE = 'sf-v1'
self.addEventListener('install', () => self.skipWaiting())
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
).then(() => self.clients.claim())
)
})
self.addEventListener('fetch', (e) => {
const req = e.request
if (req.method !== 'GET' || new URL(req.url).origin !== self.location.origin) return
// network-first: always try fresh, fall back to cache offline
e.respondWith(
fetch(req)
.then((res) => {
const copy = res.clone()
caches.open(CACHE).then((c) => c.put(req, copy))
return res
})
.catch(() => caches.match(req).then((hit) => hit || caches.match('/')))
)
})