-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsw.js
75 lines (73 loc) · 1.84 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
(function() {
'use strict';
const cacheName = '1.2.2',
offlinePage = '/',
filesToCache = [
'/',
'/index.php/css/view/index',
'/index.php/js/view/index',
'/skin/fonts/icomoon.ttf',
'/skin/fonts/icomoon.eot#iefix',
'/skin/fonts/icomoon.woff',
'/skin/fonts/icomoon.svg#icomoon',
'/ckeditor/ckeditor.js'
];
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(cacheName).then(
function(cache) {
filesToCache.map(function(url) {
return cache.add(url);
});
}
)
);
});
self.addEventListener('fetch', function(e) {
if (!e.request.url.match(/^(http|https):\/\//i)) {
return;
}
if (new URL(e.request.url).origin !== location.origin) {
return;
}
if (e.request.method !== 'GET') {
e.respondWith(fetch(e.request).catch(function() {
return caches.match(offlinePage);
}));
return;
}
if (e.request.mode === 'navigate' && navigator.onLine) {
e.respondWith(fetch(e.request).then(
function(response) {
return caches.open(cacheName).then(
function(cache) {
cache.put(e.request, response.clone());
return response;
}
);
}
));
return;
}
e.respondWith(
caches.match(e.request).then(
function(response) {
return response || fetch(e.request).then(
function(response) {
return caches.open(cacheName).then(
function(cache) {
cache.put(e.request, response.clone());
return response;
}
);
}
);
}
).catch(
function() {
return caches.match(offlinePage);
}
)
);
});
}(this));