-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
53 lines (49 loc) · 1.01 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
52
53
const appVersion = "v1.0.3";
// Files to cache
const files = [
'./',
'./index.html',
'./css/styles.css',
'./js/main.js',
'./js/jquery.min.js',
'./fonts/Arimo.ttf',
'./icon/favicon.ico',
'./icon/app-icon-96x96.png'
]
// Install
self.addEventListener('install', event => {
event.waitUntil(
caches.open(appVersion)
.then(cache => {
return cache.addAll(files)
})
.catch(err => console.log('Error adding files to cache', err))
)
console.info('SW installed')
self.skipWaiting();
})
// Activate
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys()
.then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if(cache !== appVersion) {
console.log("Deleting old caches", cache)
return caches.delete(cache)
}
})
)
})
)
return self.clients.claim();
})
// Fetch
self.addEventListener('fetch', function(event) {
event.respondWith(
fetch(event.request).catch(function() {
return caches.match(event.request);
})
);
});