-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
36 lines (33 loc) · 1.16 KB
/
sw.js
File metadata and controls
36 lines (33 loc) · 1.16 KB
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
/* QTVIS Business OS — Service Worker v1.0
Strategy: cache-first, network-update-in-background
Bump CACHE_VER whenever you push an update to GitHub */
var CACHE_VER = 'my-os-v1.0';
var SHELL = ['./', './manifest.json', './sw.js', './icon.svg'];
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(CACHE_VER).then(function(c) { return c.addAll(SHELL); })
);
self.skipWaiting();
});
self.addEventListener('activate', function(e) {
e.waitUntil(
caches.keys().then(function(keys) {
return Promise.all(keys.filter(function(k) { return k !== CACHE_VER; }).map(function(k) { return caches.delete(k); }));
}).then(function() { return clients.claim(); })
);
});
self.addEventListener('fetch', function(e) {
if (e.request.method !== 'GET') return;
e.respondWith(
caches.match(e.request).then(function(cached) {
var network = fetch(e.request).then(function(res) {
if (res.ok) {
var copy = res.clone();
caches.open(CACHE_VER).then(function(c) { c.put(e.request, copy); });
}
return res;
}).catch(function() { return cached; });
return cached || network;
})
);
});