-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
116 lines (101 loc) · 3.4 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict';
/**
* Service Worker of PWA
* To learn more and add one to your website, visit - https://PWA.com
*/
const cacheName = 'pwa-chace';
const offlinePage = self.location.protocol + '//' + self.location.host + '/';
const filesToCache = [
'/',
'./index.php',
'./login.php',
'./about.php'
];
const neverCacheUrls = [/\/contao/, /\/contao-manager/, /intall/, /manifest.webmanifest /];
// Install
self.addEventListener('install', function (e) {
console.log('PWA service worker installation');
e.waitUntil(
setTimeout(() => {
addEventListener('message', event => {
caches.open(cacheName).then(function (cache) {
console.log('PWA service worker caching dependencies');
event.data.map(function (url) {
return cache.add(url).catch(function (reason) {
//return console.log('PWA static Caching error: ' + reason + ' ' + url);
});
});
})
})
}, 1000)
);
});
//Activate and deleted old cached
self.addEventListener('activate', function (e) {
console.log('PWA service worker activation');
e.waitUntil(
caches.keys().then(function (keyList) {
return Promise.all(keyList.map(function (key) {
if (key !== cacheName) {
console.log('PWA old cache removed', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
// Fetch
self.addEventListener('fetch', function (e) {
// Return if the current request url is in the never cache list
if (!neverCacheUrls.every(checkNeverCacheList, e.request.url)) {
console.log('PWA: Current request is excluded from cache.');
return;
}
// Return if request url protocal isn't http or https
if (!e.request.url.match(/^(http|https):\/\//i))
return;
// Return if request url is from an external domain.
if (new URL(e.request.url).origin !== location.origin)
return;
// For POST requests, do not use the cache. Serve offline page if offline.
if (e.request.method !== 'GET') {
e.respondWith(
fetch(e.request).catch(function () {
return caches.match(e.request).then(res => res)
})
);
return;
}
// Revving strategy
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(e.request).then(res => res)
})
);
});
// Check if current url is in the neverCacheUrls list
function checkNeverCacheList(url) {
if (this.match(url)) {
return false;
}
return true;
}