Add PWA support for MischLabs
All checks were successful
Build & Push Docker Image to Gitea Registry / build-and-push (push) Successful in 11s

This commit is contained in:
Kroonk
2026-05-21 13:56:14 +02:00
parent 81ddb781ca
commit 1ecbcc38ae
11 changed files with 178 additions and 2 deletions

56
sw.js Normal file
View File

@@ -0,0 +1,56 @@
const CACHE_NAME = 'mischlabs-pwa-v1';
const APP_SHELL = [
'/',
'/index.html',
'/style.css?v=8',
'/manifest.webmanifest',
'/icons/icon-192.png',
'/icons/icon-512.png',
'/icons/apple-touch-icon.png'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(APP_SHELL))
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => (
Promise.all(keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key)))
))
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
const request = event.request;
if (request.method !== 'GET') {
return;
}
if (request.mode === 'navigate') {
event.respondWith(
fetch(request).catch(() => caches.match('/index.html'))
);
return;
}
event.respondWith(
caches.match(request).then((cached) => (
cached || fetch(request).then((response) => {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
const responseToCache = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, responseToCache));
return response;
})
))
);
});