-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathservice-worker.js
68 lines (65 loc) · 2.34 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
58
59
60
61
62
63
64
65
66
67
68
var log = console.log.bind(console); //bind our console to a variable
var version = "0.0.1";
var cacheName = "swETH";
var cache = cacheName + "-" + version;
var filesToCache = [
"vendor/bootstrap/css/bootstrap.min.css",
"vendor/font-awesome/css/font-awesome.min.css",
"vendor/jquery/jquery.min.js",
"vendor/bootstrap/js/bootstrap.bundle.min.js",
"img/bg.mov",
"img/*.svg" //Note that this is different from below
];
//Add event listener for install
self.addEventListener("install", function(event) {
log("[ServiceWorker] Installing....");
event.waitUntil(
caches
.open(cache) //open this cache from caches and it will return a Promise
.then(function(cache) {
//catch that promise
log("[ServiceWorker] Caching files");
cache.addAll(filesToCache); //add all required files to cache it also returns a Promise
})
);
});
//Add event listener for fetch
self.addEventListener("fetch", function(event) {
//note that event.request.url gives URL of the request so you could also intercept the request and send a response based on your URL
//e.g. you make want to send gif if anything in jpeg form is requested.
event.respondWith(
//it either takes a Response object as a parameter or a promise that resolves to a Response object
caches
.match(event.request) //If there is a match in the cache of this request object
.then(function(response) {
if (response) {
log("Fulfilling " + event.request.url + " from cache.");
//returning response object
return response;
} else {
log(event.request.url + " not found in cache fetching from network.");
//return promise that resolves to Response object
return fetch(event.request);
}
})
);
});
self.addEventListener("activate", function(event) {
log("[ServiceWorker] Activate");
event.waitUntil(
caches
.keys() //it will return all the keys in the cache as an array
.then(function(keyList) {
//run everything in parallel using Promise.all()
Promise.all(
keyList.map(function(key) {
if (key !== cacheName) {
log("[ServiceWorker] Removing old cache ", key);
//if key doesn`t matches with present key
return caches.delete(key);
}
})
);
})
);
});