-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
40 lines (37 loc) · 1000 Bytes
/
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
var CACHE_NAME = 'Invader_caches';
var appShellFiles = [
'/Invader/index.html',
'/Invader/images/app.png'
];
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(appShellFiles);
})
);
});
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(CACHE_NAME).then(function(cache) {
cache.put(e.request, response.clone());
return response;
});
});
})
);
});
self.addEventListener("activate", function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (CACHE_NAME !== cacheName && cacheName.startsWith("Invader-caches")) {
return caches.delete(cacheName);
}
})
);
})
);
});