-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
59 lines (54 loc) · 1.93 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
54
55
56
57
58
59
const currentCacheName = "cache-v2";
const resourcesToCache = [
'index.html',
'images/icon-48.png',
'/'
]
self.addEventListener('install',function(event) {
//if skipWaiting is called, the activate event will directly called after install
//maybe your application can have a problem with this skipWaiting
self.skipWaiting();
//fires when sw is installed
console.log("Service worker installed!");
//wait (synch function) till the cache is filled
event.waitUntil(
caches.open(currentCacheName)
.then(function(cache) {
return cache.addAll(resourcesToCache);
})
);
});
//activate event will be thrown for example when something inside the service worker changed and the new service worker will be activated.
//but you have to visit the page again to activate it (refresh is not enougth)
// https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle
self.addEventListener('activate', function(event) {
//console.log(caches);
console.log("Service worker activated.");
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
// Return true if you want to remove this cache,
// but remember that caches are shared across
// the whole origin
if(cacheName !== currentCacheName){
return true;
}
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
self.addEventListener('fetch', function(event) {
//first try to get everything from over the network
//if it fails try to get it from the cache.
event.respondWith(async function() {
try {
return await fetch(event.request);
} catch (err) {
return caches.match(event.request);
}
}());
});