-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
service-worker.js
51 lines (48 loc) · 1.67 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
const cacheName = 'v2.18';
const offlineUrl = '/offline.html';
const offlineFiles = [
'/index.html',
'/offline.html',
'/manifest.json',
'/css/damn.css',
'https://fonts.googleapis.com/css?family=Roboto+Slab:100,400,300,700|Roboto:400,300,700,400italic',
'/js/libraries/jquery-3.3.1.js',
'/js/libraries/vue.min.js',
'/js/min/damn.vue.min.js',
'/svg/offline-dog.svg'
];
self.addEventListener('install', function(event) {
var offlineRequest = new Request(offlineFiles);
event.waitUntil(
fetch(offlineRequest).then(function(response) {
return caches.open('offline').then(function(cache) {
console.log('[oninstall] Cached offline page', response.url);
return cache.put(offlineRequest, response);
});
})
);
});
self.addEventListener('fetch', function(event) {
// Only fall back for HTML documents.
var request = event.request;
// && request.headers.get('accept').includes('text/html')
if (request.method === 'GET') {
// `fetch()` will use the cache when possible, to this examples
// depends on cache-busting URL parameter to avoid the cache.
event.respondWith(
fetch(request).catch(function(error) {
// `fetch()` throws an exception when the server is unreachable but not
// for valid HTTP responses, even `4xx` or `5xx` range.
console.error(
'[onfetch] Failed. Serving cached offline fallback ' +
error
);
return caches.open('offline').then(function(cache) {
return cache.match(offlineUrl);
});
})
);
}
// Any other handlers come here. Without calls to `event.respondWith()` the
// request will be handled without the ServiceWorker.
});