-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
363 lines (329 loc) · 15 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
const CACHE_NAME_PERMANENT = 'cache-permanent-v1';
const CACHE_NAME_AUTO = 'cache-auto-v1';
const CACHE_NAME_MAX_AGE = 'cache-max-age-v1';
const CACHE_NAME_ALWAYS_UPDATE = 'cache-always-update-v1';
var filesToCache, permanentFiles, alwaysUpdateFiles, maxAgeFiles
let CACHE_CONFIG = null;
async function openDatabase() {
return new Promise((resolve, reject) => {
const request = indexedDB.open('cacheConfigDB', 1);
request.onupgradeneeded = event => {
const db = event.target.result;
db.createObjectStore('config', { keyPath: 'id' });
};
request.onsuccess = event => {
resolve(event.target.result);
};
request.onerror = event => {
reject(event.target.error);
};
});
}
async function getConfigFromDB(db) {
return new Promise((resolve, reject) => {
const transaction = db.transaction('config', 'readonly');
const store = transaction.objectStore('config');
const request = store.get('config');
request.onsuccess = () => {
resolve(request.result ? request.result.data : null);
};
request.onerror = () => {
console.log("load error")
reject(request.error);
};
});
}
async function saveConfigToDB(db, config) {
return new Promise((resolve, reject) => {
const transaction = db.transaction('config', 'readwrite');
const store = transaction.objectStore('config');
const itemToSave = { id: 'config', data: config };
// Log the data structure before saving
console.debug('Saving to DB:', itemToSave);
const request = store.put(itemToSave); // Store the object
request.onsuccess = () => resolve();
request.onerror = () => {
console.log("save error")
reject(request.error);
};
});
}
// Fetch the cache configuration from /cache/toCache.json
async function fetchCacheConfig() {
try {
// Check if the configuration exists in local storage
const db = await openDatabase();
let cachedConfig = await getConfigFromDB(db);
if (cachedConfig) {
// If it exists, parse it
CACHE_CONFIG = cachedConfig;
// compare versions
const response = await fetch('/cache/toCache.json');
if (response.status === 200) {
const cacheConfig = await response.json();
if (cacheConfig.version !== CACHE_CONFIG.version) {
CACHE_CONFIG = cacheConfig
if (Array.isArray(CACHE_CONFIG.force)) {
for (let i = 0; i < CACHE_CONFIG.force.length; i++) {
if (typeof CACHE_CONFIG.force[i] === "string") {
CACHE_CONFIG.force[i] = { path: CACHE_CONFIG.force[i], maxAge: -1 };
}
}
} else {
CACHE_CONFIG.force = collectFilesToCache(CACHE_CONFIG.force);
}
await saveConfigToDB(db, CACHE_CONFIG); // Save the fetched configuration
filesToCache = CACHE_CONFIG.force;
console.debug('Files to cache:', filesToCache.map(file => file.path));
permanentFiles = filesToCache.filter(file => file.maxAge === -1);
console.debug('Files to cache (permanent):', permanentFiles.map(file => file.path));
alwaysUpdateFiles = filesToCache.filter(file => file.maxAge === 0);
console.debug('Files to cache (always update):', alwaysUpdateFiles.map(file => file.path));
maxAgeFiles = filesToCache.filter(file => file.maxAge > 0);
console.debug('Files to cache (max Age):', maxAgeFiles.map(file => file.path));
console.debug("Refetch all cached data and updated config!")
fetchAllData()
return
}
}
console.debug('Cache config loaded from local storage:', CACHE_CONFIG);
} else {
// If not, fetch it from the server
const response = await fetch('/cache/toCache.json');
CACHE_CONFIG = await response.json();
if (Array.isArray(CACHE_CONFIG.force)) {
for (let i = 0; i < CACHE_CONFIG.force.length; i++) {
if (typeof CACHE_CONFIG.force[i] === "string") {
CACHE_CONFIG.force[i] = { path: CACHE_CONFIG.force[i], maxAge: -1 };
}
}
} else {
CACHE_CONFIG.force = collectFilesToCache(CACHE_CONFIG.force);
}
await saveConfigToDB(db, CACHE_CONFIG); // Save the fetched configuration
console.debug('Cache config loaded from server:', CACHE_CONFIG);
}
} catch (error) {
console.error('Failed to fetch cache configuration:', error);
}
filesToCache = CACHE_CONFIG.force;
console.debug('Files to cache:', filesToCache.map(file => file.path));
permanentFiles = filesToCache.filter(file => file.maxAge === -1);
console.debug('Files to cache (permanent):', permanentFiles.map(file => file.path));
alwaysUpdateFiles = filesToCache.filter(file => file.maxAge === 0);
console.debug('Files to cache (always update):', alwaysUpdateFiles.map(file => file.path));
maxAgeFiles = filesToCache.filter(file => file.maxAge > 0);
console.debug('Files to cache (max Age):', maxAgeFiles.map(file => file.path));
}
async function fetchAllData() {
const permanentCache = await caches.open(CACHE_NAME_PERMANENT);
const permanentUrlsToCache = permanentFiles.map(file => file.path);
try {
for (const url of permanentUrlsToCache) {
const response = await fetch(url);
if (!response.ok) {
console.error(`Failed to fetch ${url}: ${response.status}`);
continue; // Skip this URL and continue with others
}
await permanentCache.add(url); // Add individual URL to cache
}
console.debug('Permanent files cached successfully');
} catch (error) {
console.error('Failed to cache permanent files:', error);
}
const alwaysCache = await caches.open(CACHE_NAME_ALWAYS_UPDATE, {
durability: 'strict', // Or `'relaxed'`.
persisted: true, // Or `false`.
});
const alwaysFilesUrlsToCache = alwaysUpdateFiles.map(file => file.path);
try {
for (const url of alwaysFilesUrlsToCache) {
const response = await fetch(url);
if (!response.ok) {
console.error(`Failed to fetch ${url}: ${response.status}`);
continue; // Skip this URL and continue with others
}
await alwaysCache.add(url); // Add individual URL to cache
}
console.debug('Always update files cached successfully');
} catch (error) {
console.error('Failed to cache always update files:', error);
}
const maxAgeCache = await caches.open(CACHE_NAME_MAX_AGE);
const maxAgeFilesUrlsToCache = maxAgeFiles.map(file => file.path);
try {
for (const url of maxAgeFilesUrlsToCache) {
const response = await fetch(url);
if (!response.ok) {
console.error(`Failed to fetch ${url}: ${response.status}`);
continue; // Skip this URL and continue with others
}
await maxAgeCache.add(url); // Add individual URL to cache
}
console.debug('Always update files cached successfully');
} catch (error) {
console.error('Failed to cache always update files:', error);
}
}
// Recursively collect files for caching based on configuration
function collectFilesToCache(structure, parentPath = '', maxAge = -1) {
let files = [];
for (const key in structure) {
if (key == "maxAge") {
if (typeof structure[key] === 'number') {
maxAge = structure[key]
continue
} else {
throw "mallformededaaaaa" // TODO: fix
}
}
if (typeof structure[key] === 'object') {
if (Object.keys(structure[key]).length === 0) {
files.push({ path: `${parentPath}${key}`, maxAge: maxAge });
} else if (
Object.keys(structure[key]).length === 1
&& structure[key].hasOwnProperty('maxAge')
) {
files.push({ path: `${parentPath}${key}`, maxAge: structure[key].maxAge })
} else {
files.push(...collectFilesToCache(structure[key], `${parentPath}${key}/`, maxAge));
}
} else {
throw "mallformededbbbb" // TODO: fix
}
}
return files;
}
// Install event: Cache permanent files and setup other caches
self.addEventListener('install', event => {
event.waitUntil(
(async () => {
console.debug('Service Worker: Installing and fetching cache config...');
await fetchCacheConfig();
if (!CACHE_CONFIG) return;
await fetchAllData();
})()
);
});
// Fetch event: Handle different caching rules based on the configuration
self.addEventListener('fetch', event => {
event.respondWith(
(async () => {
if (!CACHE_CONFIG) {
await fetchCacheConfig();
}
const requestUrl = new URL(event.request.url);
// Permanent cache: Serve from the permanent cache if no other rules apply
if (permanentFiles.some(file => requestUrl.pathname.endsWith(file.path))) { //TODO: DOMAIN
const permanentCache = await (await caches.open(CACHE_NAME_PERMANENT)).match(event.request, { ignoreVary: true });
if (permanentCache) {
console.debug(`Serving from permanent cache: ${requestUrl.href}`);
return permanentCache;
} else {
const response = await fetch(event.request);
const cache = await caches.open(CACHE_NAME_ALWAYS_UPDATE);
try {
await cache.put(event.request, response.clone());
console.debug(`Added to permanent cache: ${requestUrl.href}`);
} catch (error) {
console.error('Failed to update permanent:', error);
}
return response;
}
}
// Always update cache
if (alwaysUpdateFiles.some(file => requestUrl.pathname.endsWith(file.path))) { //TODO: DOMAIN
const cache = await caches.open(CACHE_NAME_ALWAYS_UPDATE);
try {
const response = await fetch(event.request);
console.debug("resons ok")
await cache.put(event.request, response.clone());
console.debug(`Always updated cache: ${requestUrl.href}`);
return response;
} catch (error) {
cachedResponse = await cache.match(event.request)
if (cachedResponse) {
console.debug(`Always update: Serving from cache as fallback: ${requestUrl.href}`);
return cachedResponse;
}
console.error('Failed to update always cache:', error);
}
}
// Max age cache
const maxAgeFile = maxAgeFiles.find(file => requestUrl.pathname.endsWith(file.path));
if (maxAgeFile) {
const cache = await caches.open(CACHE_NAME_MAX_AGE);
const cachedResponse = await cache.match(event.request, { ignoreVary: true });
if (cachedResponse) {
const cachedDate = new Date(cachedResponse.headers.get('date'));
const ageInHours = (Date.now() - cachedDate.getTime()) / 1000 / 60 / 60;
if (ageInHours < maxAgeFile.maxAge) {
console.debug(`Serving from max-age cache: ${requestUrl.href}`);
return cachedResponse;
}
}
const networkResponse = await fetch(event.request);
try {
await cache.put(event.request, networkResponse.clone());
console.debug(`Max-age cache updated: ${requestUrl.href}`);
} catch (error) {
cachedResponse = await cache.match(event.request, { ignoreVary: true })
if (cachedResponse) {
console.debug(`Max-age: Serving from cache as fallback: ${requestUrl.href}`);
return cachedResponse;
}
console.error('Failed to update max-age cache:', error);
}
return networkResponse;
}
// Automatic caching for autoCacheDomains
if (CACHE_CONFIG.autoCache) {
const autoCacheDomain = CACHE_CONFIG.autoCacheDomains.length === 0 || CACHE_CONFIG.autoCacheDomains.some(domain => new RegExp(domain).test(requestUrl.origin));
if (autoCacheDomain) {
// Handle preventAutoCache logic
if (CACHE_CONFIG.preventAutoCache.some(pattern => new RegExp(pattern).test(requestUrl.href))) {
console.debug(`Prevented caching of ${requestUrl.href}`);
return fetch(event.request);
}
const cache = await caches.open(CACHE_NAME_AUTO);
const cachedResponse = await cache.match(event.request, { ignoreVary: true });
if (cachedResponse) {
console.debug(`Serving from auto cache: ${requestUrl.href}`);
return cachedResponse;
}
const networkResponse = await fetch(event.request);
try {
await cache.put(event.request, networkResponse.clone());
console.debug(`Auto cache updated: ${requestUrl.href}`);
} catch (error) {
console.error('Failed to update auto cache:', error);
}
return networkResponse;
}
}
console.debug("remote get", requestUrl);
return fetch(event.request);
})()
);
});
// Activate event: Clean up old caches
self.addEventListener('activate', event => {
const cacheWhitelist = [
CACHE_NAME_PERMANENT,
CACHE_NAME_AUTO,
CACHE_NAME_MAX_AGE,
CACHE_NAME_ALWAYS_UPDATE
];
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (!cacheWhitelist.includes(cacheName)) {
console.debug(`Deleting old cache: ${cacheName}`);
return caches.delete(cacheName);
}
})
);
})
);
});