-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
95 lines (87 loc) · 2.75 KB
/
sw.js
File metadata and controls
95 lines (87 loc) · 2.75 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const CACHEVERSION = 'v20';
const CACHEDSTATICASSETS = [
'/',
'/index.html',
'/favicon.ico'
];
if ('serviceWorker' in navigator) {
navigator.serviceWorker.getRegistrations().then(function (registrations) {
//returns installed service workers
if (registrations.length) {
for(let registration of registrations) {
console.log(registration);
registration.unregister();
}
}
});
}
self.addEventListener('install', function(event) {
caches.open(CACHEVERSION).then(function(cache) {
cache.addAll(CACHEDSTATICASSETS)
}).then(self.skipWaiting());
});
self.addEventListener('activate', function(event) {
const currentCaches = [CACHEDSTATICASSETS];
event.waitUntil(
caches.keys().then(function(cacheNames) {
return cacheNames.filter(function(cacheName) {
!currentCaches.includes(cacheName)
});
}).then(function(cachesToDelete) {
return Promise.all(cachesToDelete.map(
function(cacheToDelete) {
return caches.delete(cacheToDelete);
}
));
}).then(function() {
self.clients.claim()
})
);
});
self.addEventListener('fetch', function(event) {
if( (event.request.url.startsWith("https://www.codesign2.co.uk")) &&
(event.request.method == "GET") ) { // Not X-domain
// Try network and if it fails, go for the cached copy.
event.respondWith(
fromNetwork(event.request, 500)
.catch(function () {
return fromCache(event.request);
})
);
}
});
function redirect(res, url) {
res.header('X-Redirect-To', url);
res.end();
}
// Time limited network request. If the network fails or the response is not
// served before timeout, the promise is rejected.
function fromNetwork(request, timeout) {
return new Promise(function (fulfill, reject) {
// Reject in case of timeout.
var timeoutId = setTimeout(reject, timeout);
fetch(request).then(function (response) {
clearTimeout(timeoutId);
if (response.ok) {
// update stored in cache
caches.open(CACHEVERSION).then(function(cache) {
cache.put(request, response);
});
// Fulfill in case of success.
fulfill(response.clone());
}
})
// Reject also if network fetch rejects.
.catch(reject);
});
}
// Open the cache where the assets were stored and search for the requested
// resource. Notice that in case of no matching, the promise still resolves
// but it does with `undefined` as value.
function fromCache(request) {
return caches.open(CACHEVERSION).then(function (cache) {
return cache.match(request).then(function (matching) {
return matching || Promise.reject('no-match');
});
});
}