-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
53 lines (46 loc) · 1.6 KB
/
Copy pathsw.js
File metadata and controls
53 lines (46 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Simple offline cache for the app shell (no bundler).
// It caches your local files so the app opens offline after first load.
// Large model files from CDNs are NOT force-cached here (browser will cache them normally).
const CACHE_NAME = 'pdc-shell-v2';
const APP_SHELL = [
'./',
'./index.html',
'./styles.css',
'./app.js',
// localForage script is external; let the browser handle its own cache
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL)).then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
(async () => {
const keys = await caches.keys();
await Promise.all(keys.map(k => (k === CACHE_NAME ? null : caches.delete(k))));
await self.clients.claim();
})()
);
});
// Cache-first for app shell, network-first for everything else.
self.addEventListener('fetch', (event) => {
const req = event.request;
const url = new URL(req.url);
// Only handle GET
if (req.method !== 'GET') return;
// App shell (same-origin files)
if (url.origin === self.location.origin && APP_SHELL.includes(url.pathname.replace(/^\.\//, '/'))) {
event.respondWith(
caches.match(req).then((cached) => cached || fetch(req))
);
return;
}
// Default: network-first with fallback to cache (helpful if offline)
event.respondWith(
fetch(req).then((resp) => {
// Optionally: put small assets into cache (skip huge models)
return resp;
}).catch(() => caches.match(req))
);
});