1
0
silverbullet/webapp/service_worker.ts

46 lines
1.3 KiB
TypeScript
Raw Normal View History

import { manifest, version } from "@parcel/service-worker";
async function install() {
const cache = await caches.open(version);
2022-04-07 13:21:30 +00:00
// console.log("Installing", manifest);
await cache.addAll(manifest);
2022-04-07 13:21:30 +00:00
// console.log("DOne");
}
//@ts-ignore
self.addEventListener("install", (e) => e.waitUntil(install()));
async function activate() {
const keys = await caches.keys();
2022-04-07 13:21:30 +00:00
// console.log("Activating");
await Promise.all(keys.map((key) => key !== version && caches.delete(key)));
2022-04-07 13:21:30 +00:00
// console.log("DOne activating");
}
//@ts-ignore
self.addEventListener("activate", (e) => e.waitUntil(activate()));
2022-04-07 13:21:30 +00:00
self.addEventListener("fetch", (event: any) => {
2022-04-10 09:04:07 +00:00
return;
2022-04-07 13:21:30 +00:00
event.respondWith(
caches.open(version).then(async (cache) => {
let parsedUrl = new URL(event.request.url);
// console.log("Got fetch request", parsedUrl.pathname);
let response = await cache.match(event.request, {
ignoreSearch: true,
});
// console.log("Got cache result", response);
if (response) {
return response;
} else {
if (
parsedUrl.pathname !== "/fs" &&
!parsedUrl.pathname.startsWith("/fs/") &&
!parsedUrl.pathname.startsWith("/plug/")
) {
return cache.match("/index.html");
}
return fetch(event.request);
}
})
);
});