-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
45 lines (42 loc) · 1.38 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
var CACHE_VERSION = 'liushisigua_7d58d0';
var CACHE_FILES = [
'/liushisigua/',
'/liushisigua/index.html',
'/liushisigua/index.js',
'/liushisigua/index.css'
];
self.addEventListener('install', function (e) {
console.log('[Service Worker] Installing');
e.waitUntil(
caches.open(CACHE_VERSION)
.then(function (cache) {
console.log('[Service Worker] Caching all content')
return cache.addAll(CACHE_FILES)
})
);
});
self.addEventListener('activate', function (e) {
e.waitUntil(
caches.keys().then(function (keys) {
return Promise.all(keys.map(function (key) {
if (CACHE_FILES.indexOf(key) === -1) {
return caches.delete(key)
}
}));
})
);
});
self.addEventListener('fetch', function (e) {
e.respondWith(
caches.match(e.request).then(function (r) {
console.log('[Service Worker] Fetching resource: ' + e.request.url);
return r || fetch(e.request).then(function (response) {
return caches.open(CACHE_FILES).then(function (cache) {
console.log('[Service Worker] Caching new resource: ' + e.request.url);
cache.put(e.request, response.clone())
return response
})
});
})
);
});