-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
58 lines (51 loc) · 1.28 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
self.importScripts('data/cards.js');
var cacheName = 'turnorder-v1';
var appShellFiles = [
'/turnorder/app.css',
'/turnorder/app.js',
'/turnorder/index.html',
'/turnorder/sw.js'
];
var cardsImages = [];
for (var i = 0; i < cards.length; i++) {
cardsImages.push('data/img/' + cards[i].file);
}
var contentToCache = appShellFiles.concat(cardsImages);
// Install serviceWorker
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll(contentToCache);
})
);
});
// Fetching content using Service Worker
self.addEventListener('fetch', function(e) {
e.respondWith(
caches.match(e.request).then(function(r) {
return (
r ||
fetch(e.request).then(function(response) {
return caches.open(cacheName).then(function(cache) {
cache.put(e.request, response.clone());
return response;
});
})
);
})
);
});
// clear old caches
self.addEventListener('activate', function(e) {
e.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(
keyList.map(function(key) {
if (cacheName.indexOf(key === -1)) {
return caches.delete(key);
}
})
);
})
);
});