-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceworker.js
84 lines (78 loc) · 2.91 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
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
75
76
77
78
79
80
81
82
83
84
/**
* Unlike most Service Workers, this one always attempts to download assets
* from the network. Only when network access fails do we fallback to using
* the cache. When a request succeeds we always update the cache with the new
* version. If a request fails and the result isn't in the cache then we
* display an Offline page.
*/
const CACHE = "content-v1"; // name of the current cache
const OFFLINE = "/offline.html"; // URL to offline HTML document
const AUTO_CACHE = [
// URLs of assets to immediately cache
OFFLINE,
"/"
];
// Iterate AUTO_CACHE and add cache each entry
self.addEventListener("install", (event) => {
event.waitUntil(
caches
.open(CACHE)
.then((cache) => cache.addAll(AUTO_CACHE))
.then(self.skipWaiting())
);
});
// Destroy inapplicable caches
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return cacheNames.filter((cacheName) => CACHE !== cacheName);
})
.then((unusedCaches) => {
console.log("DESTROYING CACHE", unusedCaches.join(","));
return Promise.all(
unusedCaches.map((unusedCache) => {
return caches.delete(unusedCache);
})
);
})
.then(() => self.clients.claim())
);
});
self.addEventListener("fetch", (event) => {
if (
!event.request.url.startsWith(self.location.origin) ||
event.request.method !== "GET"
) {
// External request, or POST, ignore
return void event.respondWith(fetch(event.request));
}
event.respondWith(
// Always try to download from server first
fetch(event.request)
.then((response) => {
// When a download is successful cache the result
caches.open(CACHE).then((cache) => {
cache.put(event.request, response);
});
// And of course display it
return response.clone();
})
.catch((_err) => {
// A failure probably means network access issues
// See if we have a cached version
return caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
// We did have a cached version, display it
return cachedResponse;
}
// We did not have a cached version, display offline page
return caches.open(CACHE).then((cache) => {
const offlineRequest = new Request(OFFLINE);
return cache.match(offlineRequest);
});
});
})
);
});