Skip to content

Commit

Permalink
Getting cached assets and dunamic caching, #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Elhamne committed Feb 20, 2023
1 parent cb684c0 commit 4ecd6c1
Showing 1 changed file with 65 additions and 45 deletions.
110 changes: 65 additions & 45 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,78 @@
/* eslint-disable array-callback-return */
/* eslint-disable no-undef */
// const staticCacheName = 'SAY-v2.0.0-beta';
// const urlsToCache = [
// '/static/js/main.chunk.js',
// '/static/js/0.chunk.js',
// '/static/js/bundle.js',
// '/static/js/vendors~main.chunk.js',
// '/static/js/main.482930e5.chunk.js',
// '/static/js/2.a637ec1e.chunk.js',
// '/images/back_gray.svg',
// '/images/back_orange.svg',
// '/images/logo.png',
// '/images/back.svg',
// '/images/icons/changePassword.svg',
// '/images/icons/email.svg',
// '/images/icons/exit.svg',
// '/images/icons/info.svg',
// '/images/icons/language.svg',
// '/images/icons/Money.svg',
// '/images/icons/settings.svg',
// '/images/icons/Task.svg',
// '/images/icons/un.svg',
// '/images/icons/upload.svg',
// '/images/icons/wallet.svg',
// '/images/icons/family.svg',
// '/images/icons/doneNeeds/child.svg',
// '/images/icons/doneNeeds/hand.svg',
// '/images/icons/doneNeeds/ngo.svg',
// '/images/icons/doneNeeds/package.svg',
// '/images/icons/doneNeeds/receipts/done.jpg',
// '/images/icons/doneNeeds/receipts/ngo_delivered.jpg',
// '/images/icons/doneNeeds/receipts/done.jpg',
// '/images/cartWhite.svg',
// '/images/favicon.png',
// '/images/finalForm.svg',
// '/images/say_donation.svg',
// '/images/back.svg',
// '/images/intro.png',
// '/images/otp.svg',
// '/images/register.svg',
// '/offline.html',
// ];
const staticCacheName = 'SAY-v2.0.0-beta';
const dynamicCacheName = 'SAY-dynamic-v1';
const urlsToCache = [
'/',
'/index.html',
'/assets/locales/translations/fa.json',
'/assets/locales/translations/en.json',
'/images/Say_donation.png',
'/images/register.svg',
'/images/otp.svg',
'/images/logo2.png',
'/images/logo.png',
'/images/intro.png',
'/images/finalForm.svg',
'/images/favicon.png',
'/images/favicon.ico',
'/images/cartWhite.svg',
'/images/back.svg',
'/images/back_orange.svg',
'/images/back_gray.svg',
'/images/icons/changePassword.svg',
'/images/icons/email.svg',
'/images/icons/exit.svg',
'/images/icons/family.svg',
'/images/icons/info.svg',
'/images/icons/language.svg',
'/images/icons/Money.svg',
'/images/icons/settings.svg',
'/images/icons/Task.svg',
'/images/icons/un.svg',
'/images/icons/upload.svg',
'/images/icons/wallet.svg',
];

// Install service worker
self.addEventListener('install', (e) => {
console.log('service worker installed');
// console.log('service worker installed');
e.waitUntil(
(async () => {
const cache = await caches.open(staticCacheName);
console.log('caching shell assets');
await cache.addAll(urlsToCache);
})()
);
});

// activate event
// activate event - deleting old caches
self.addEventListener('activate', (e) => {
console.log('service worker has been activated');
// console.log('service worker has been activated');
e.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys
.filter((key) => key !== staticCacheName)
.map((key) => caches.delete(key))
);
})
);
});

// fetch event
self.addEventListener('fetch', (e) => {
console.log('fetch event', e);
// console.log('fetch event', e);
e.respondWith(
(async () => {
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;
})()
);
});

0 comments on commit 4ecd6c1

Please sign in to comment.