-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
76 lines (73 loc) · 2.17 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
let cacheName = "radiovnik-v3";
let siteUrl = "simp.jachyhm.cz";
let filesToCache = [
"/",
"/data.json",
"/favicon.ico",
"/manifest.webmanifest",
"/assets/css/bootstrap.min.css",
"/assets/css/hierarchy-select.min.css",
"/assets/css/solid.min.css",
"/assets/css/style.css",
"/assets/icons/favicon-128x128.png",
"/assets/icons/favicon-144x144.png",
"/assets/icons/favicon-152x152.png",
"/assets/icons/favicon-192x192.png",
"/assets/icons/favicon-384x384.png",
"/assets/icons/favicon-512x512.png",
"/assets/icons/favicon-72x72.png",
"/assets/icons/favicon-96x96.png",
"/assets/webfonts/fa-solid-900.eot",
"/assets/webfonts/fa-solid-900.svg",
"/assets/webfonts/fa-solid-900.ttf",
"/assets/webfonts/fa-solid-900.woff",
"/assets/webfonts/fa-solid-900.woff2",
"/js/bootstrap.bundle.min.js",
"/js/descriptors.js",
"/js/hierarchy-select.min.js",
"/js/jquery-3.6.0.min.js",
"/js/main.js",
"/js/stationRow.js",
];
AbortSignal.timeout ??= function timeout(ms) {
const ctrl = new AbortController()
setTimeout(() => ctrl.abort(), ms)
return ctrl.signal
}
self.addEventListener("install", function(e) {
e.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll(filesToCache)
})
)
})
self.addEventListener("fetch", function(e) {
var url = new URL(e.request.url);
var host = url.hostname;
var fileURI = url.pathname;
const cleanURL = `${url.protocol}//${url.host}${url.pathname}`;
if (host == siteUrl && filesToCache.includes(fileURI)) {
e.respondWith(
caches.open(cacheName).then(function(cache) {
return fetch(e.request, { signal: AbortSignal.timeout(5000) }).then((response) => {
if (response.status < 400) {
cache.put(cleanURL, response.clone());
} else {
cache.delete(cleanURL);
}
console.log(`Returning online version of ${cleanURL}`);
return response;
},
() => {
console.log(`Returning cached version of ${cleanURL}`);
return cache.match(cleanURL);
}).catch((err) => {
console.log(`Fetch vailed with "${err}"\r\nReturning cached version of ${cleanURL}`);
return cache.match(cleanURL);
});
})
)
} else {
console.log(`Fallback to default behaviour for ${cleanURL}`);
}
})