You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
offline-octopus/examples/pwa/sw.js

44 lines
1.0 KiB

const cacheAssets = [
'/pwa/index.html',
'/pwa/testdata.json',
]
const cacheName = '00004';
// Call Install Event
self.addEventListener('install', e => {
console.log('Service Worker: Installed');
e.waitUntil(
async () => {
cache = await caches.open(cacheName);
console.log('Service Worker: Caching Files', cache);
await cache.addAll(cacheAssets);
self.skipWaiting();
}
);
});
// Call Activate Event
self.addEventListener('activate', e => {
console.log('Service Worker: Activated');
// Remove unwanted caches
e.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if (cache !== cacheName) {
console.log('Service Worker: Clearing Old Cache');
return caches.delete(cache);
}
})
);
})
);
});
// Call Fetch Event
self.addEventListener('fetch', e => {
console.log('Service Worker: Fetching');
e.respondWith(fetch(e.request).catch(() => caches.match(e.request)));
});