integration: add site/sw.js
Browse files- site/sw.js +45 -0
site/sw.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Sovereign site service worker — app-shell cache for the PWA.
|
| 2 |
+
* Cache-first for the static shell; network-first for API calls. */
|
| 3 |
+
const CACHE = 'sovereign-v1';
|
| 4 |
+
const SHELL = [
|
| 5 |
+
'./',
|
| 6 |
+
'index.html',
|
| 7 |
+
'dashboard.html',
|
| 8 |
+
'chat.html',
|
| 9 |
+
'pec5d.html',
|
| 10 |
+
'kyrexis.html',
|
| 11 |
+
'coldcase.html',
|
| 12 |
+
'manifest.webmanifest'
|
| 13 |
+
];
|
| 14 |
+
|
| 15 |
+
self.addEventListener('install', (event) => {
|
| 16 |
+
event.waitUntil(
|
| 17 |
+
caches.open(CACHE).then((cache) => cache.addAll(SHELL)).then(() => self.skipWaiting())
|
| 18 |
+
);
|
| 19 |
+
});
|
| 20 |
+
|
| 21 |
+
self.addEventListener('activate', (event) => {
|
| 22 |
+
event.waitUntil(
|
| 23 |
+
caches.keys().then((keys) =>
|
| 24 |
+
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))
|
| 25 |
+
).then(() => self.clients.claim())
|
| 26 |
+
);
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
self.addEventListener('fetch', (event) => {
|
| 30 |
+
const url = new URL(event.request.url);
|
| 31 |
+
// API calls: network-first, no cache fallback (fresh data matters)
|
| 32 |
+
if (url.pathname.includes('/api/')) {
|
| 33 |
+
return;
|
| 34 |
+
}
|
| 35 |
+
event.respondWith(
|
| 36 |
+
caches.match(event.request).then((cached) => {
|
| 37 |
+
if (cached) return cached;
|
| 38 |
+
return fetch(event.request).then((response) => {
|
| 39 |
+
const clone = response.clone();
|
| 40 |
+
caches.open(CACHE).then((cache) => cache.put(event.request, clone));
|
| 41 |
+
return response;
|
| 42 |
+
});
|
| 43 |
+
})
|
| 44 |
+
);
|
| 45 |
+
});
|