-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
75 lines (73 loc) · 2.37 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
75
let version = 'v4.2.2';
self.addEventListener('install', evt => {
console.log('Service worker ' + version + ' is being installed');
evt.waitUntil(preload());
});
self.addEventListener('activate', evt => {
console.log('Service worker ' + version + ' is being activated');
evt.waitUntil(
caches.keys().then(keylist => {
return Promise.all(keylist.map(key => {
if (key.startsWith('flownotes-') && (key != 'flownotes-' + version)) return caches.delete(key);
}));
})
);
});
self.addEventListener('fetch', evt => {
if (evt.request.url.match(/\/data.php(\?.*)?$/)) return;
if (evt.request.url.match(/\/share.html/)) {
evt.respondWith(
tryCache(evt.request, { ignoreSearch: true }).catch(() => tryNetwork(evt.request, 60000))
);
}
else if (evt.request.cache && ((evt.request.cache == 'no-cache') || (evt.request.cache == 'reload'))) {
evt.respondWith(
tryNetwork(evt.request, 60000, { cache: 'reload' }).catch(() => tryCache(evt.request))
);
}
else {
evt.respondWith(
tryCache(evt.request).catch(() => tryNetwork(evt.request, 60000))
);
}
});
self.addEventListener('message', evt => {
if (evt.data.action == 'skipWaiting') {
self.skipWaiting();
}
else if (evt.data.action == 'refresh') {
preload().then(evt.source.postMessage({ action: 'reload' }));
}
});
async function preload() {
return caches.open('flownotes-' + version).then(cache => {
let files = [
'./',
'./script.js',
'./style.css',
'./share.html',
'./popout.html'
];
return cache.addAll(files.map((file) => new Request(file, { cache: 'reload' })));
});
}
function tryNetwork(request, timeout, options = {}) {
return new Promise((fulfill, reject) => {
let id = setTimeout(reject, timeout);
fetch(request, options).then(response => {
clearTimeout(id);
fulfill(response.clone());
if (response.status == 200) {
if (request.method == 'GET') caches.open('flownotes-' + version).then(cache => cache.put(request, response));
}
else console.log(`Request for ${request.url} failed with status ${response.status} (${response.statusText})`);
}, reject);
});
}
function tryCache(request, options = {}) {
return caches.open('flownotes-' + version).then(
cache => cache.match(request, options).then(
matching => matching || Promise.reject('no-match')
)
);
}