Nox / sw.js
Umer359's picture
Claude Sonnet 5
Add voice input, read-aloud, and a backgrounded-tab reply flash
04d7e2a
Raw History Blame Contribute Delete
1.79 kB
// Minimal offline app-shell cache — deliberately narrow: only the small set
// of same-origin static files that make up the page itself. Never touches
// the chat API (cross-origin, and never GET) or the CDN libraries (marked,
// DOMPurify, pdf.js, highlight.js, the Google Font) — those should always
// go through the browser's normal cache/network so a library update or a
// worker deploy is picked up immediately, not stuck behind a stale SW cache.
const CACHE_NAME = 'nox-shell-v2';
const APP_SHELL = [
'./',
'./index.html',
'./manifest.json',
'./devseis-logo.png',
'./icon-192.png',
'./icon-192-alert.png',
'./icon-512.png',
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)).catch(() => {})
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)))
)
);
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;
if (!APP_SHELL.some((p) => url.pathname.endsWith(p.replace('./', '')) || (p === './' && url.pathname === '/'))) return;
// Network-first so a redeploy is visible on next load when online; falls
// back to the cached shell only when the network actually fails (offline).
event.respondWith(
fetch(req)
.then((res) => {
const copy = res.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(req, copy)).catch(() => {});
return res;
})
.catch(() => caches.match(req))
);
});