-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
86 lines (80 loc) · 2.71 KB
/
Copy pathservice-worker.js
File metadata and controls
86 lines (80 loc) · 2.71 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
79
80
81
82
83
84
85
86
/* =====================================================
BoltType — service-worker.js
Cache-first strategy for full offline support.
===================================================== */
const CACHE_NAME = 'bolttype-v1.3';
const CACHE_URLS = [
'index.html',
'style.css',
'app.js',
'BoltType.png',
'manifest.json'
];
// ─── Install: pre-cache all core assets ───────────────
self.addEventListener('install', event => {
console.log('[SW] Installing and caching assets...');
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
return cache.addAll(CACHE_URLS);
})
.then(() => {
console.log('[SW] All assets cached successfully.');
// Force the waiting SW to become the active SW immediately
return self.skipWaiting();
})
.catch(err => console.error('[SW] Cache install failed:', err))
);
});
// ─── Activate: remove old caches ──────────────────────
self.addEventListener('activate', event => {
console.log('[SW] Activating and cleaning old caches...');
event.waitUntil(
caches.keys()
.then(cacheNames => {
return Promise.all(
cacheNames
.filter(name => name !== CACHE_NAME)
.map(name => {
console.log('[SW] Deleting old cache:', name);
return caches.delete(name);
})
);
})
.then(() => {
// Claim all clients so the updated SW takes effect immediately
return self.clients.claim();
})
);
});
// ─── Fetch: Cache-First, fallback to network ──────────
self.addEventListener('fetch', event => {
// Only handle GET requests
if (event.request.method !== 'GET') return;
event.respondWith(
caches.match(event.request)
.then(cachedResponse => {
if (cachedResponse) {
// Return cached asset immediately
return cachedResponse;
}
// Not in cache — try the network
return fetch(event.request)
.then(networkResponse => {
// Cache a copy of valid responses for future use
if (networkResponse && networkResponse.status === 200) {
const responseClone = networkResponse.clone();
caches.open(CACHE_NAME)
.then(cache => cache.put(event.request, responseClone));
}
return networkResponse;
})
.catch(() => {
// Offline and not cached — return offline fallback if applicable
if (event.request.destination === 'document') {
return caches.match('index.html');
}
});
})
);
});