-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
57 lines (54 loc) · 1.61 KB
/
service-worker.js
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
const CACHE_NAME = 'tip-calculator-cache-v1';
const urlsToCache = [
'/tipcalc/',
'/tipcalc/index.html',
'/tipcalc/styles.css',
'/tipcalc/app.js',
'/tipcalc/assets/icon-192x192.png',
'/tipcalc/assets/icon-512x512.png',
'/tipcalc/assets/android-icon-36x36.png',
'/tipcalc/assets/android-icon-48x48.png',
'/tipcalc/assets/android-icon-72x72.png'
];
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => {
// Return the cached response if found, otherwise fetch from network
return response || fetch(event.request).then(networkResponse => {
// Cache the new response for future requests
return caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
});
}).catch(() => {
// Fallback to a default offline page or asset if both cache and network fail
if (event.request.mode === 'navigate') {
return caches.match('/tipcalc/index.html');
}
})
);
});
self.addEventListener('activate', event => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});