-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
74 lines (69 loc) · 2.36 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
let cacheName = "v1";
// Assets to cache
var assetsToCache = [
"./index.html",
"./dist/output.css",
"./src/js/main.js",
"./src/js/toggle.js",
"./src/js/sweetalert.min.js",
"./src/images/icons8-calculator-64.png",
];
self.addEventListener("install", function (event) {
// waitUntil() ensures that the Service Worker will not
// install until the code inside has successfully occurred
event.waitUntil(
// Create cache with the name supplied above and
// return a promise for it
caches
.open(cacheName)
.then(function (cache) {
// Important to `return` the promise here to have `skipWaiting()`
// fire after the cache has been updated.
return cache.addAll(assetsToCache);
})
.then(function () {
// `skipWaiting()` forces the waiting ServiceWorker to become the
// active ServiceWorker, triggering the `onactivate` event.
// Together with `Clients.claim()` this allows a worker to take effect
// immediately in the client(s).
return self.skipWaiting();
})
);
});
// Activate event
// Be sure to call self.clients.claim()
self.addEventListener("activate", function (event) {
// `claim()` sets this worker as the active worker for all clients that
// match the workers scope and triggers an `oncontrollerchange` event for
// the clients.
return self.clients.claim();
});
self.addEventListener("fetch", function (event) {
// Ignore non-get request like when accessing the admin panel
if (event.request.method !== "GET") {
return;
}
// Don't try to handle non-secure assets because fetch will fail
if (/http:/.test(event.request.url)) {
return;
}
// Here's where we cache all the things!
event.respondWith(
// Open the cache created when install
caches.open(cacheName).then(function (cache) {
// Go to the network to ask for that resource
return fetch(event.request)
.then(function (networkResponse) {
// Add a copy of the response to the cache (updating the old version)
cache.put(event.request, networkResponse.clone());
// Respond with it
return networkResponse;
})
.catch(function () {
// If there is no internet connection, try to match the request
// to some of our cached resources
return cache.match(event.request);
});
})
);
});