-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
40 lines (34 loc) · 1.22 KB
/
Copy pathsw.js
File metadata and controls
40 lines (34 loc) · 1.22 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
const CACHE_NAME = 'lb-core-v1';
self.addEventListener('install', event => {
// Skip waiting to activate immediately
self.skipWaiting();
});
self.addEventListener('activate', event => {
// Claim clients immediately
event.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', event => {
// Only intercept basic HTTP GET requests
if (event.request.method !== 'GET') return;
event.respondWith(
fetch(event.request).then(response => {
// Ensure response is valid before caching
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
// Clone the response stream and cache the latest version
const responseToCache = response.clone();
caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, responseToCache);
});
return response;
}).catch(() => {
// Network failed (offline), fallback to cache
return caches.match(event.request).then(cachedResponse => {
if (cachedResponse) {
return cachedResponse;
}
});
})
);
});