-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserviceworker.js
66 lines (62 loc) · 1.72 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
const version = "2"
const CACHE = version + "-onprem-wtf-offline";
self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(CACHE).then(function (cache) {
// non critical
cache.addAll([
'/assets/js/index.json',
]);
// critical
return cache.addAll([
'/',
'/index.html',
'/offline',
'/assets/js/theme.js',
'/Tags',
'/PowerShell',
'/post',
'/search',
'/assets/WOFF2/TTF/SourceSerifPro-Light.ttf.woff2',
'/assets/WOFF2/TTF/SourceSans3-Light.ttf.woff2',
'/assets/WOFF2/OTF/SourceCodePro-Regular.otf.woff2',
'/assets/WOFF2/TTF/SourceSans3-Regular.ttf.woff2'
]);
}),
);
});
self.addEventListener('fetch', function (event) {
event.respondWith(
caches.open(CACHE).then(function (cache) {
return cache.match(event.request).then(function (response) {
var fetchPromise = fetch(event.request)
.then(function (networkResponse) {
cache.put(event.request, networkResponse.clone());
return networkResponse;
})
.catch( error => {
console.log("not found: " + error);
return caches.match('/offline');
});
return response || fetchPromise;
});
}),
);
});
self.addEventListener("activate", function(event) {
event.waitUntil(
caches
.keys()
.then(function(keys) {
return Promise.all(
keys
.filter(function(key) {
return !key.startsWith(version);
})
.map(function(key) {
return caches.delete(key);
})
);
})
);
});