-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
78 lines (70 loc) · 2.04 KB
/
Copy pathsw.js
File metadata and controls
78 lines (70 loc) · 2.04 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Focus Timer Service Worker
// Caches the app shell so it loads fast; dynamic content (sessions, log) always hits network
const CACHE_NAME = 'focus-timer-v1';
// Files to cache on install — the app shell
const SHELL = [
'./',
'./index.html',
'./manifest.json',
'./icons/icon-192.png',
'./icons/icon-512.png',
'https://fonts.googleapis.com/css2?family=Bebas+Neue&family=DM+Sans:wght@300;400;500&display=swap'
];
// Never cache these — always fetch from network
const NETWORK_ONLY = [
'sessions.php',
'log.php',
'library.php',
'log-page.php',
'claude-focus.php',
'upload_video.php'
];
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(SHELL);
})
);
self.skipWaiting();
});
self.addEventListener('activate', function(e) {
// Remove old caches
e.waitUntil(
caches.keys().then(function(keys) {
return Promise.all(
keys.filter(function(k) { return k !== CACHE_NAME; })
.map(function(k) { return caches.delete(k); })
);
})
);
self.clients.claim();
});
self.addEventListener('fetch', function(e) {
var url = new URL(e.request.url);
// Network-only for PHP endpoints
var isNetworkOnly = NETWORK_ONLY.some(function(path) {
return url.pathname.includes(path);
});
if (isNetworkOnly) {
e.respondWith(fetch(e.request));
return;
}
// Cache-first for everything else (app shell, fonts)
e.respondWith(
caches.match(e.request).then(function(cached) {
return cached || fetch(e.request).then(function(response) {
// Cache successful GET responses for the shell
if (e.request.method === 'GET' && response.status === 200) {
var clone = response.clone();
caches.open(CACHE_NAME).then(function(cache) {
cache.put(e.request, clone);
});
}
return response;
});
}).catch(function() {
// Offline fallback — return cached index if available
return caches.match('./index.html');
})
);
});