implement update prompt, clean service worker handling

This commit is contained in:
marlon-erler
2026-09-24 22:07:36 +02:00
parent ecf72a7318
commit a63488d62a
10 changed files with 625 additions and 930 deletions
Vendored
+35 -14
View File
@@ -1,32 +1,53 @@
const CACHE_VERSION = "3";
const VERSION = "2610 Beta 1"
self.addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event));
function processUrl(event) {
const url = event.request.url;
const fileName = url.split("/").pop();
return {url, fileName};
}
self.addEventListener("install", () => {
self.skipWaiting();
});
async function handleRequest(event) {
const response = await getFromCache(event);
if (response) {
fetchAndCache(event); // Call this but don't await, allowing the cache to update in the background
return response;
self.addEventListener("fetch", async (event) => {
console.log(VERSION);
const {url, fileName}=processUrl(event);
event.respondWith(handleRequest(event, url, fileName));
});
async function handleRequest(event, url, fileName) {
switch (fileName) {
case "version":
const response = new Response(VERSION);
return response;
case "latestVersion":
return await fetch("/version.txt", {cache: "no-cache"})
}
return fetchAndCache(event);
const response = await getFromCache(event);
if (response) {
fetchAndCache(event, fileName); // Call this but don't await, allowing the cache to update in the background
return response;
}
return fetchAndCache(event, fileName);
}
async function getFromCache(event) {
const request = event.request;
const cache = await caches.open(CACHE_VERSION);
const cache = await caches.open(VERSION);
const response = await cache.match(request);
return response;
}
async function fetchAndCache(event) {
async function fetchAndCache(event, fileName) {
const request = event.request;
const response = await fetch(request, { cache: "no-cache" });
if (response.status === 200) {
// Only cache successful responses
const cache = await caches.open(CACHE_VERSION);
cache.put(request, response.clone());
// Only cache successful responses
const cache = await caches.open(VERSION);
await cache.put(request, response.clone());
}
return response;
}