-
Notifications
You must be signed in to change notification settings - Fork 1
/
service_worker.js
84 lines (76 loc) · 2.6 KB
/
service_worker.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
// 2021-05-03, v1
var CACHE_START_TEXT = 'CACHE MANIFEST',
CACHE_END_TEXT = 'NETWORK';
/**
* Delete all old service worker caches.
* @returns {Promise} Resolves when all have been deleted
*/
function deleteOldCaches() {
return caches.keys().then(function (cacheNames) {
var cacheDeletionPromises = cacheNames.map(function (cacheName) {
return caches.delete(cacheName);
});
return Promise.all(cacheDeletionPromises);
});
}
self.addEventListener('install', function(ev) {
ev.waitUntil(fetch('/manifest.appcache')
.then(function (appCacheRequest) {
return appCacheRequest.text();
}).then(function (appCacheText) {
// Remove any Windows line endings.
appCacheText = appCacheText.replace(/\r/g, '');
var startIndex = appCacheText.indexOf(CACHE_START_TEXT) + CACHE_START_TEXT.length + 1,
endIndex = appCacheText.indexOf(CACHE_END_TEXT) - 2;
if (endIndex === -3) { endIndex = appCacheText.length - 1; }
var appCacheArray = appCacheText
// Take only the CACHE MANIFEST section.
.substring(startIndex, endIndex)
// Remove any extra line breaks.
.trim()
// And split the list into an array.
.split('\n'),
// Get the cache name from the first line; the rest will be the list of URLs.
cacheName = appCacheArray.splice(0, 1)[0].substring(2);
// Precede each URL with a “/”.
appCacheArray = appCacheArray.map(function (url) { return '/' + url });
return deleteOldCaches()
.then(function () {
return caches.open(cacheName)
})
.then(function (cache) {
// Add files individually to make debugging easier if something goes wrong.
var cachePromises = appCacheArray.map(function (url) {
return cache.add(url)
.then(function () {
console.log('Added to service worker cache: ' + url);
}).catch(function (err) {
console.error('Failed to add file to service worker cache: ' + url);
console.error(err);
});
});
return Promise.all(cachePromises);
}).then(function () {
console.log('Finished adding files to service worker cache.');
});
}));
});
self.addEventListener('fetch', function(ev) {
// Do not attempt to load external requests from cache.
if (ev.request.url.indexOf(self.origin) !== 0) {
return fetch(ev.request);
}
var url = ev.request.url.split('?')[0].split('#')[0];
// Make any URL to a directory look for index.html in that directory.
if (url.substr(-1) === '/') {
url += 'index.html';
}
ev.respondWith(caches.match(url)
.then(function(response) {
if (response) {
return response;
}
return fetch(ev.request);
}));
});