From 4ecd6c10af418b13baa697406f120e64e87a2c5c Mon Sep 17 00:00:00 2001 From: Elhamne Date: Mon, 20 Feb 2023 13:24:42 +0330 Subject: [PATCH] Getting cached assets and dunamic caching, #11 --- public/sw.js | 110 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 45 deletions(-) diff --git a/public/sw.js b/public/sw.js index 6fc1adac..0511ed83 100644 --- a/public/sw.js +++ b/public/sw.js @@ -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; + })() + ); });