-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
76 lines (67 loc) · 2.06 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
69
70
71
72
73
74
75
76
'use strict';
// https://tosbourn.com/adding-service-worker-to-jekyll/
const offlineCache = 'offline-2023-12-23'; // The cache key, you can override this to manually clear the cache
const offlinePage = '/offline/'; // Where your offline page lives
const homePage = '/'; // A key page you want cached
const about = '/about/'; // Another key page to cache
const tagsmenu = "/tags-menu/";
const tagLM = "/tag/L&M";
const tagWhoAmI = "/tag/Who-am-i";
const series_intro_page = "/2022/03/08/series-introduction.html"
// Delete caches that do not match the current version of the service worker.
function clearOldCaches() {
return caches.keys().then(keys => {
return Promise.all(
keys
.filter(key => key.indexOf(offlineCache) !== 0)
.map(key => caches.delete(key))
);
});
}
// Precache specific pages
function cacheOfflinePage() {
return caches.open(offlineCache)
.then(cache => {
return cache.addAll([offlinePage, homePage, about, tagsmenu, tagLM, tagWhoAmI, series_intro_page]);
})
.catch(error => {})
}
// Install the service worker
self.addEventListener('install', event => {
event.waitUntil(
cacheOfflinePage()
.then( () => self.skipWaiting() )
);
});
// Activate the service worker
self.addEventListener('activate', event => {
event.waitUntil(
clearOldCaches()
.then(() => self.clients.claim())
);
});
// Try and serve up cached page, or else show networked page, or else show offline page
self.addEventListener('fetch', event => {
let request = event.request;
event.respondWith(
caches.match(request)
.then(response => {
return response || fetch(request)
.then( response => {
// NETWORK
if (response && response.ok) {
let copy = response.clone();
caches.open(offlineCache)
.then( cache => cache.put(request, copy) );
}
return response;
})
.catch( error => {
// OFFLINE
if (request.mode == 'navigate') {
return caches.match(offlinePage);
}
});
})
);
});