-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsw.js
52 lines (45 loc) · 1.14 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
var CACHE = "noten-lernen-cache-2";
var precacheFiles = [
"/",
"/index.html",
"/static/manifest.json",
"/static/favicon.png"
];
self.addEventListener("install", function(evt) {
evt.waitUntil(
precache().then(function() {
return self.skipWaiting();
})
);
});
self.addEventListener("activate", function(event) {
return self.clients.claim();
});
self.addEventListener("fetch", function(evt) {
evt.respondWith(fromCache(evt.request).catch(fromServer(evt.request)));
evt.waitUntil(update(evt.request));
});
function precache() {
return caches.open(CACHE).then(function(cache) {
return cache.addAll(precacheFiles);
});
}
function fromCache(request) {
return caches.open(CACHE).then(function(cache) {
return cache.match(request).then(function(matching) {
return matching || Promise.reject("no-match");
});
});
}
function update(request) {
return caches.open(CACHE).then(function(cache) {
return fetch(request).then(function(response) {
return cache.put(request, response);
});
});
}
function fromServer(request) {
return fetch(request).then(function(response) {
return response;
});
}