2023-06-21 14:56:12 +01:00
|
|
|
/// <reference types="@sveltejs/kit" />
|
|
|
|
|
|
|
|
import { build, files, version } from '$service-worker';
|
|
|
|
|
|
|
|
const cacheName = `ob-${version}`;
|
|
|
|
|
|
|
|
const assets = [
|
|
|
|
...build,
|
2023-06-22 21:15:09 +01:00
|
|
|
...files,
|
|
|
|
"/service-worker.js"
|
2023-06-21 14:56:12 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
self.addEventListener('install', (event) => {
|
|
|
|
async function addToCache() {
|
|
|
|
const cache = await caches.open(cacheName);
|
|
|
|
await cache.addAll(assets);
|
|
|
|
}
|
|
|
|
|
|
|
|
event.waitUntil(addToCache());
|
|
|
|
})
|
|
|
|
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
|
|
async function deleteOldCache() {
|
|
|
|
for (const key of await caches.keys()) {
|
|
|
|
if (key !== cacheName) {
|
|
|
|
await caches.delete(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event.waitUntil(deleteOldCache());
|
|
|
|
})
|
|
|
|
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
|
|
if (event.request.method !== 'GET') {return}
|
|
|
|
async function respond() {
|
|
|
|
const cacheRes = await caches.match(event.request, {ignoreSearch: true});
|
|
|
|
if (cacheRes) {
|
|
|
|
return cacheRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await fetch(event.request)
|
|
|
|
} catch (err) {
|
|
|
|
return {"error": "OFFLINE", "errorMsg": "You are not online"};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event.respondWith(respond());
|
|
|
|
})
|