-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsw-v0.js
43 lines (37 loc) · 1.17 KB
/
sw-v0.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
var staticCacheName = 'myNewsSite-v0';
self.addEventListener('install', function (event) {
console.log('ServiceWorker (' + staticCacheName + '): install called');
event.waitUntil(
caches.open(staticCacheName).then(function (cache) {
return cache.addAll([
'/',
'index.html',
'manifest.json',
'lib/jquery.min.js',
'lib/bootstrap.min.css',
'custom.css',
'feedReader.js',
]);
})
);
});
self.addEventListener('activate', function (event) {
console.log('ServiceWorker: Activate');
//activate active worker asap
event.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', function (event) {
//handle live reload function (for develop purpose)
if (event.request.url.indexOf('/browser-sync/') !== -1) {
//fetch(..) is the new XMLHttpRequest
event.respondWith(fetch(event.request));
return;
}
console.log('ServiceWorker: fetch called for ' + event.request.url);
//if request in cache then return it, otherwise fetch it from the network
event.respondWith(
caches.match(event.request).then(function (response) {
return response || fetch(event.request);
})
);
});