diff --git a/app/static/app/js/serviceworker.js b/app/static/app/js/serviceworker.js index be5ba1f..9975220 100644 --- a/app/static/app/js/serviceworker.js +++ b/app/static/app/js/serviceworker.js @@ -1,21 +1,47 @@ -var staticCacheName = "django-pwa-VI" + new Date().getTime(); -var filesToCache = [ - '/app/home', -]; -// self.addEventListener("install", event => { -// this.skipWaiting(); -// event.waitUntil( -// caches.open(staticCacheName) -// .then(cache => { -// return fetch('/app/home') -// .then(response => cache.put('/app/home', new Response(response.body))); -// }) -// ) -// }); +var staticCacheName = "django-pwa-v" + new Date().getTime(); + +self.addEventListener("install", event => { + this.skipWaiting(); + + const urlsToCache = [ + '/app/home/', + '/static/app/css/bootstrap.css', + '/static/app/css/custom.css', + '/static/app/js/dbcontrol.js', + '/static/app/js/home.js', + '/static/app/js/timer.js', + '/static/app/js/welcomeControl.js', + '/static/app/images/refresh.png', + '/static/app/images/play.png', + '/static/app/images/pause.png', + '/static/app/images/stop.png', + '/static/app/images/addcircle.png' + ]; + + event.waitUntil( + caches.open(staticCacheName) + .then(cache => { + // Array von Promises, um alle Ressourcen zu cachen + return Promise.all( + urlsToCache.map(url => { + return fetch(url, { redirect: 'follow' }).then(response => { + // Prüfe, ob die Antwort erfolgreich und nicht umgeleitet wurde + if (response.ok && !response.redirected) { + return cache.put(url, response); + } + }); + }) + ); + }) + ); +}); // Clear cache on activate self.addEventListener('activate', event => { + + event.waitUntil(self.clients.claim()); + event.waitUntil( caches.keys().then(cacheNames => { return Promise.all( @@ -28,15 +54,15 @@ self.addEventListener('activate', event => { ); }); -// // Serve from Cache -// self.addEventListener("fetch", event => { -// event.respondWith( -// caches.match(event.request) -// .then(response => { -// return response || fetch(event.request); -// }) -// .catch(() => { -// return caches.match('app/home'); -// }) -// ) -// }); \ No newline at end of file +// Serve from Cache +self.addEventListener("fetch", event => { + event.respondWith( + caches.match(event.request) + .then(response => { + return response || fetch(event.request); + }) + .catch(() => { + return caches.match('/app/home'); + }) + ) +}); \ No newline at end of file diff --git a/app/templates/app/home.html b/app/templates/app/home.html index 18c2bab..2b77135 100644 --- a/app/templates/app/home.html +++ b/app/templates/app/home.html @@ -128,7 +128,7 @@
Dauer
Datum
Optionen:
Dauer
Datum
Optionen: