70 lines
1.5 KiB
JavaScript
70 lines
1.5 KiB
JavaScript
const CACHE_NAME = 'vbb-status-v29';
|
|
const urlsToCache = [
|
|
'/',
|
|
'/index.html',
|
|
'/manifest.json',
|
|
'/DotMatrix.ttf',
|
|
'/images/favicon.png'
|
|
];
|
|
|
|
// Installation
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
console.log('Cache geöffnet');
|
|
return cache.addAll(urlsToCache);
|
|
})
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
// Aktivierung
|
|
self.addEventListener('activate', event => {
|
|
event.waitUntil(
|
|
caches.keys().then(cacheNames => {
|
|
return Promise.all(
|
|
cacheNames.map(cacheName => {
|
|
if (cacheName !== CACHE_NAME) {
|
|
console.log('Alter Cache wird gelöscht:', cacheName);
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
})
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
// Fetch
|
|
self.addEventListener('fetch', event => {
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(response => {
|
|
// Cache hit - return response
|
|
if (response) {
|
|
return response;
|
|
}
|
|
|
|
return fetch(event.request).then(
|
|
response => {
|
|
// Check if valid response
|
|
if (!response || response.status !== 200 || response.type !== 'basic') {
|
|
return response;
|
|
}
|
|
|
|
// Clone the response
|
|
const responseToCache = response.clone();
|
|
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
cache.put(event.request, responseToCache);
|
|
});
|
|
|
|
return response;
|
|
}
|
|
);
|
|
})
|
|
);
|
|
});
|