39 lines
702 B
JavaScript
39 lines
702 B
JavaScript
const cacheName = 'pwa-conf-v4';
|
|
const staticAssets = [
|
|
'./app.js ',
|
|
'./websocket.js' //TODO: add all necessary files
|
|
|
|
];
|
|
|
|
|
|
self.addEventListener('install', async event => {
|
|
const cache = await caches.open(cacheName);
|
|
|
|
|
|
await cache.addAll(staticAssets);
|
|
|
|
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
const req = event.request;
|
|
event.respondWith(networkFirst(req));
|
|
});
|
|
|
|
async function cacheFirst(req) {
|
|
const cache = await caches.open(cacheName);
|
|
|
|
|
|
const cachedResponse = await cache.match(req);
|
|
|
|
|
|
return cachedResponse || fetch(req);
|
|
|
|
|
|
}
|
|
|
|
async function networkFirst(req) {
|
|
return fetch(req).catch(function() {
|
|
return caches.match(req);
|
|
})
|
|
} |