-
Notifications
You must be signed in to change notification settings - Fork 245
/
service-worker.js
52 lines (47 loc) · 1.31 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
'use strict';
const OFFLINE_CACHE = 'offline';
const OFFLINE_URL = '/errors/offline.html';
importScripts('/scripts/sw-cache-polyfill.js');
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(OFFLINE_CACHE).then(function(cache) {
return cache.addAll([
OFFLINE_URL,
'/styles/screen.css?v=fresh',
'/images/github.svg',
'/images/logo.png',
'/images/[email protected]',
'/scripts/highlight.js',
'/scripts/salvattore.js'
]);
})
);
});
self.addEventListener('fetch', function(event) {
if (
event.request.method == 'GET' &&
event.request.headers.get('accept').includes('text/html')
) {
// It’s a GET request for an HTML document.
//console.log('Handling fetch event for', event.request.url);
event.respondWith(
fetch(event.request).catch(function(exception) {
// The `catch` is only triggered if `fetch()` throws an exception,
// which most likely happens due to the server being unreachable.
console.error(
'Fetch failed; returning offline page instead.',
exception
);
return caches.open(OFFLINE_CACHE).then(function(cache) {
return cache.match(OFFLINE_URL);
});
})
);
} else {
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
}
});