-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
203 lines (166 loc) · 5.23 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const pageDomain = "ts5viewer.endercentral.eu";
const cacheVersion = 7;
const beta = pageDomain !== location.hostname;
const currentCacheKey = `v${cacheVersion}${beta ? "-beta" : ""}`;
//Resources pre-cached
const precacheResources = [
//Html & CSS
"/",
"/style.css",
//Manifest
"/manifest.json",
//JS Files
"/js/App.js",
"/js/EnvironmentChecker.js",
"/js/Logger.js",
"/js/Main.js",
"/js/PreloadedElements.js",
"/js/ServiceWorkerRegisterer.js",
"/js/UrlParamReader.js",
"/js/Utils.js",
"/js/data_loaders/InterfaceAppLoader.js",
"/js/data_loaders/ObsAppLoader.js",
"/js/data_loaders/StandaloneAppLoader.js",
"/js/interface/Interface.js",
"/js/ts/remote_app/RemoteAppConnection.js",
"/js/ts/Channel.js",
"/js/ts/Client.js",
"/js/ts/Handler.js",
"/js/ts/Server.js",
"/js/viewer/ChannelView.js",
"/js/viewer/ClientView.js",
"/js/viewer/ServerView.js",
"/js/viewer/View.js",
"/js/viewer/Viewer.js",
//Data
"/data/interface_app.json",
"/data/standalone_app.json",
"/data/obs_app.json",
//Resources
// Fonts
"/resources/font/roboto/font-family.css",
"/resources/font/roboto/Roboto-Regular.ttf",
// Images
"/resources/img/audio_input.svg",
"/resources/img/client_status/audio_input_muted_hardware.svg",
"/resources/img/client_status/audio_input_muted_local.svg",
"/resources/img/client_status/audio_input_muted.svg",
"/resources/img/client_status/audio_output_muted.svg",
"/resources/img/client_status/away_and_sound_muted.svg",
"/resources/img/client_status/away.svg",
];
//Resources that are fetched first and if the fetch fails, are then loaded from cache
const fetchFirstResources = [
];
//Event Listeners
self.addEventListener("install", function(event) {
event.waitUntil(addPrecacheResources());
});
self.addEventListener("activate", function(event) {
event.waitUntil(deleteOldCaches());
});
self.addEventListener("fetch", function(event) {
const options = {
request: event.request,
};
if(event.request.method !== "GET") {
event.respondWith(fetchOnly(options));
return;
}
if(location.hostname !== pageDomain && !event.request.url.includes("/resources/")) {
worker.log("Detected Page on alternative Domain, fetching first ...");
event.respondWith(fromFetchFirst(options));
return;
}
for(const fetchFirstRessource of fetchFirstResources) {
if(event.request.url.endsWith(fetchFirstRessource)) {
event.respondWith(fromFetchFirst(options));
return;
}
}
event.respondWith(fromCacheFirst(options));
});
//Utilty Functions
async function addPrecacheResources() {
worker.log("Adding Precache Resources ...");
const cache = await caches.open(currentCacheKey);
worker.log("Cache '" + currentCacheKey + "' opened");
for(const resource of precacheResources) {
try {
const request = new Request(resource, {
cache: "no-store",
});
const cached = await caches.match(request);
if(cached) {
continue;//Do not try to re-cache already cached things
}
worker.log("Caching '" + resource + "' ...");
await cache.add(request);
} catch(err) {
worker.log(`Could not cache '${resource}'.`);
}
}
worker.log("Files cached!");
}
async function fromCacheFirst({request}) {
try {
const cached = await caches.match(request);
if(cached) {
return cached;
}
const fetchResponse = await fetch(request, {
cache: "no-store",
});
addToCache(request, fetchResponse.clone());
return fetchResponse;
} catch(error) {
return new Response("Network Error", {
status: 408,
headers: {"Content-Type": "text/plain"},
});
}
}
async function fromFetchFirst({request}) {
try {
const fetchResponse = await fetch(request, {
cache: "no-store",
});
addToCache(request, fetchResponse.clone());
return fetchResponse;
} catch(error) {
const cached = await caches.match(request);
if(cached) {
return cached;
}
return new Response("Network Error", {
status: 408,
headers: {"Content-Type": "text/plain"},
});
}
}
async function fetchOnly({request}) {
return await fetch(request, {
cache: "no-store",
});
}
async function addToCache(request, response) {
const cache = await caches.open(currentCacheKey);
cache.put(request, response);
}
async function deleteOldCaches() {
worker.log("Deleting Old Caches ...");
const keys = await caches.keys();
const cachesToDelete = keys.filter(function(key) {
return key !== currentCacheKey;
});
await Promise.all(cachesToDelete.map(deleteCache));
}
async function deleteCache(key) {
worker.log("Deleting Cache '" + key + "'");
await caches.delete(key);
}
const worker = {
log: function(data) {
console.log("[Service Worker]", data);
}
};