-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsw-v1.js
61 lines (54 loc) · 1.78 KB
/
sw-v1.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
59
60
61
var staticCacheName = 'myNewsSite-v1';
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');
event.waitUntil(deleteOldCacheVersions().then(function () {
self.clients.claim();
}));
function deleteOldCacheVersions() {
return caches.keys().then(function (keyList) {
return Promise.all(keyList.map(function (key) {
if (key !== staticCacheName) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
});
}
});
self.addEventListener('fetch', function (event) {
//handle browser-sync develop function
if (event.request.url.indexOf('/browser-sync/') !== -1) {
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
//the request from the network is cached for further usings.
event.respondWith(
caches.match(event.request).then(function (responseFromCache) {
return responseFromCache || fetch(event.request).then(function (responseFresh) {
var responseToCache = responseFresh.clone();
caches.open(staticCacheName).then(function (cache) {
return cache.put(event.request, responseToCache);
});
return responseFresh;
});
})
);
});