51 lines
No EOL
1.3 KiB
JavaScript
51 lines
No EOL
1.3 KiB
JavaScript
const CACHE_NAME = 'memaps-v1';
|
|
const urlsToCache = [
|
|
'/',
|
|
'/map',
|
|
'/usage',
|
|
'/contact',
|
|
'/assets/bootstrap/bootstrap.rtl.min.css',
|
|
'/assets/leaflet/leaflet.css',
|
|
'/assets/leaflet/leaflet.js',
|
|
'/favicon.svg',
|
|
'/site.webmanifest'
|
|
];
|
|
|
|
// Install event
|
|
self.addEventListener('install', function(event) {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(function(cache) {
|
|
console.log('Opened cache');
|
|
return cache.addAll(urlsToCache);
|
|
})
|
|
);
|
|
});
|
|
|
|
// Fetch event
|
|
self.addEventListener('fetch', function(event) {
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(function(response) {
|
|
// Return cached version or fetch from network
|
|
return response || fetch(event.request);
|
|
}
|
|
)
|
|
);
|
|
});
|
|
|
|
// Activate event
|
|
self.addEventListener('activate', function(event) {
|
|
event.waitUntil(
|
|
caches.keys().then(function(cacheNames) {
|
|
return Promise.all(
|
|
cacheNames.map(function(cacheName) {
|
|
if (cacheName !== CACHE_NAME) {
|
|
console.log('Deleting old cache:', cacheName);
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
})
|
|
);
|
|
});
|