49 lines
1.1 KiB
JavaScript
49 lines
1.1 KiB
JavaScript
|
/// <reference types="@sveltejs/kit" />
|
||
|
|
||
|
import { build, files, version } from '$service-worker';
|
||
|
|
||
|
const cacheName = `ob-${version}`;
|
||
|
|
||
|
const assets = [
|
||
|
...build,
|
||
|
...files
|
||
|
];
|
||
|
|
||
|
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());
|
||
|
})
|