-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
80 lines (72 loc) · 2.32 KB
/
sw.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
var CACHE_NAME = 'n3n-cache';
var REQUIRED_FILES = [
'/',
'/mizui.css',
'/mizui.js',
'/script.js',
'/favicon.ico',
];
self.addEventListener('install', function (event) {
// Put `offline.html` page into cache
var offlineRequest = new Request('offline/index.html');
event.waitUntil(cacheOffline(offlineRequest));
// Perform install steps
event.waitUntil(precache());
});
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
);
}).then(fromCache(request)).finally(fetchOffline())
);
}
// Any other handlers come here. Without calls to `event.respondWith()` the
// request will be handled without the ServiceWorker.
// event.respondWith(fromCache(event.request));
event.waitUntil(update(request));
});
function precache() {
return caches.open(CACHE_NAME).then(function (cache) {
console.log("Using cache " + CACHE_NAME);
return cache.addAll(REQUIRED_FILES)
})
}
function cacheOffline(offlineRequest) {
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);
});
})
}
function fetchOffline() {
return caches.open('offline').then(function (cache) {
return cache.match('offline/index.html');
});
}
function fromCache(request) {
return caches.open(CACHE_NAME).then(function (cache) {
return cache.match(request).then(function (matching) {
return matching || Promise.reject('no-match');
})
})
}
function update(request) {
return caches.open(CACHE_NAME).then(function (cache) {
return fetch(request).then(function (response) {
return cache.put(request, response);
});
});
}