Skip to content

Commit

Permalink
completed fetch event, #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Elhamne committed Feb 21, 2023
1 parent 4ecd6c1 commit c7d28e9
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions public/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const dynamicCacheName = 'SAY-dynamic-v1';
const urlsToCache = [
'/',
'/index.html',
'/offline.html',
'/assets/locales/translations/fa.json',
'/assets/locales/translations/en.json',
'/images/Say_donation.png',
Expand Down Expand Up @@ -50,10 +51,10 @@ self.addEventListener('install', (e) => {
self.addEventListener('activate', (e) => {
// console.log('service worker has been activated');
e.waitUntil(
caches.keys().then((keys) => {
caches.keys().then((keyList) => {
return Promise.all(
keys
.filter((key) => key !== staticCacheName)
keyList
.filter((key) => key !== staticCacheName && key !== dynamicCacheName)
.map((key) => caches.delete(key))
);
})
Expand All @@ -65,14 +66,20 @@ self.addEventListener('fetch', (e) => {
// console.log('fetch event', e);
e.respondWith(
(async () => {
const r = await caches.match(e.request);
if (r) {
return r;
try {
const r = await caches.match(e.request);
if (r) {
return r;
}
const response = await fetch(e.request);
const cache = await caches.open(dynamicCacheName);
await cache.put(e.request.url, response.clone());
return response;
} catch {
if (e.request.url.indexOf('.html') > -1) {
return caches.match('/offline.html');
}
}
const response = await fetch(e.request);
const cache = await caches.open(dynamicCacheName);
await cache.put(e.request.url, response.clone());
return response;
})()
);
});

0 comments on commit c7d28e9

Please sign in to comment.