-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserviceWorker.js
46 lines (37 loc) · 1.19 KB
/
serviceWorker.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
//
// Reference https://www.pwabuilder.com/serviceworker and https://www.youtube.com/watch?v=ksXwaWHCW6k
//
(function myServiceWorker(){
const cacheName = "myCache";
const cacheAssets = ['app.html', 'doc.html' , 'index.html', './assets/favicon.ico'];
function activateHandler(event){
console.log("ServiceWorker: activated")
}
function fetchHandler(event){
console.log("ServiceWorker: fetching requested resources");
event.respondWith(
fetch(event.request)
.catch(function(){
window.caches.match(event.request)
})
);
}
function installHandler(event){
console.log("ServiceWorker: installed");
event.waitUntil(
caches.open(cacheName).then(function (cache){
console.log("ServiceWorker: files cached");
cache.addAll(cacheAssets);
})
.then(function() {
self.skipWaiting;
})
);
}
// Handle the INSTALL event; cache files for offline use
self.addEventListener("install", installHandler);
// Handle the ACTIVATE event; delete old caches
self.addEventListener("activate", activateHandler);
// If any fetch fails, it will show the offline page.
self.addEventListener("fetch", fetchHandler);
})();