-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
104 lines (95 loc) · 2.98 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
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
/*!
* Generic Service Worker for Thorium projects
* Version 3.4 january, 2022
* Copyright 2018-2022 Nymphide Lab.
*/
const origin=self.location.origin;
const skipAllExternalUrl=true;
const blacklist=["https://firestore.googleapis.com","version.json",".php","%7B%7Binstimage%7D%7D"];
const preloadList=['./','./offline.html', './index.html', './manifest.json']
function isRejectable(url) {
if ((skipAllExternalUrl == true) && (url.indexOf(origin) == -1)) {
return true;
} else {
if (blacklist.some(v => url.includes(v))) {
return true;
} else {
return false;
}
}
}
self.addEventListener('install', function(event) {
event.waitUntil(preLoad());
console.log('[com.thorium.serviceworker] Service Worker Initialized');
});
/* -- SW Initialization --*/
var preLoad = function(){
console.log('[com.thorium.serviceworker] Service Worker Installation');
return caches.open('thorium-cache')
.then(function(cache) {
return cache.addAll(preloadList);
});
}
self.addEventListener('activate', function(event) {
console.log('[com.thorium.serviceworker] service worker activated');
});
/* -- SW Fetch during use --*/
self.addEventListener('fetch', function(event) {
if ( isRejectable(event.request.url)==true) {
console.log('[com.thorium.serviceworker] request rejected '+event.request.url);
return;
}
event.respondWith(
checkResponse(event.request)
.catch(function() {
console.log('[com.thorium.serviceworker] file returned from cache: '+event.request.url);
return returnFromCache(event.request);
}
));
event.waitUntil(
addToCache(event.request)
);
});
var checkResponse = function(request){
return new Promise(function(fulfill, reject) {
fetch(request)
.then(function(response){
if(response.status !== 404) {
console.log('[com.thorium.serviceworker] Response Status '+response.status+": "+request.url);
fulfill(response)
} else {
console.log("[com.thorium.serviceworker] reject response for url "+request.url);
reject();
}
}, reject)
});
};
var addToCache = function (request) {
try {
return caches.open('thorium-cache').then(function (cache) {
return fetch(request)
.then(function (response) {
console.log('com.thorium.serviceworker] file added to cache ' + request.url);
return cache.put(request, response);
});
});
}
catch(err) {
console.log('com.thorium.serviceworker] error ' + err.message);
}
};
var returnFromCache = function (request) {
return caches.open('thorium-cache')
.then(function (cache) {
return cache.match(request)
.then(function (matching) {
if (!matching || matching.status == 404) {
console.log("[com.thorium.serviceworker] offline page");
return cache.match('offline.html');
} else {
console.log("[com.thorium.serviceworker] cache returned " + request.url);
return matching
}
});
});
};