33 lines
838 B
JavaScript
33 lines
838 B
JavaScript
const CACHE_NAME = 'lips-pwa';
|
|
const urlsToCache = ['/',
|
|
'/index.html',
|
|
'/app.scm',
|
|
'/manifest.json',
|
|
'https://cdn.jsdelivr.net/npm/lips@beta/dist/lips.min.js',
|
|
'https://cdnjs.cloudflare.com/ajax/libs/mithril/2.2.2/mithril.min.js'];
|
|
|
|
// Install event: cache the assets
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
console.log('Opened cache');
|
|
return cache.addAll(urlsToCache);
|
|
})
|
|
);
|
|
});
|
|
|
|
// Fetch event: serve cached assets when offline
|
|
self.addEventListener('fetch', event => {
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(response => {
|
|
// Cache hit - return the response
|
|
if (response) {
|
|
return response;
|
|
}
|
|
// Otherwise, fetch from network
|
|
return fetch(event.request);
|
|
})
|
|
);
|
|
});
|