-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
51 lines (48 loc) · 1.67 KB
/
sw.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
// service worker file
const staticCacheName = 'calculator-v5.6.9';
const assets = [
'/smart-calculator/',
'/smart-calculator/index.html',
'/smart-calculator/style/style.css',
'/smart-calculator/script/script.js',
'/smart-calculator/script/app.js',
'/smart-calculator/img/favicon.png',
'/smart-calculator/style/fonts/tahoma.ttf',
'https://fonts.googleapis.com/css2?family=Poppins:wght@500&family=Charm&display=swap',
'https://fonts.gstatic.com/s/charm/v5/7cHmv4oii5K0MdY8K-4E4Q.woff2',
'https://fonts.gstatic.com/s/charm/v5/7cHmv4oii5K0MdYoK-4.woff2',
'https://fonts.gstatic.com/s/poppins/v15/pxiByp8kv8JHgFVrLGT9Z1xlFQ.woff2'
];
// install service worker
self.addEventListener('install', event => {
// console.log('service worker has been installed');
// pre-caching assets
event.waitUntil(
caches.open(staticCacheName).then(cache => {
// console.log('caching shell assets');
cache.addAll(assets)
})
);
});
// activate event
self.addEventListener('activate', event => {
// console.log('service worker has been activated');
// delete previous caches
event.waitUntil(
caches.keys().then(keys => {
return Promise.all(keys
.filter(key => key !== staticCacheName)
.map(key => caches.delete(key))
)
})
);
});
// fetch event
self.addEventListener('fetch', event => {
// console.log('fetch event', event);
event.respondWith(
caches.match(event.request).then(cacheResponse => {
return cacheResponse || fetch(event.request);
})
);
})