diff --git a/src/index.ts b/src/index.ts index 81de2e4..3c59d9f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,11 +7,27 @@ console.log("%cWARNING!!!" + "color: red; font-size: 30px;", "color: black; font-size: 15px;"); -// Service worker registration -if("serviceWorker" in navigator) { - navigator.serviceWorker.register("sw.js").then(registration => { - console.log("Service worker registration successful with scope: ", registration.scope); - }).catch(err => { - console.log("Service worker registration failed: ", err); - }); -} \ No newline at end of file +// Service worker registration for offline support +const registerServiceWorker = async () => { + if ('serviceWorker' in navigator) { + try { + const registration = await navigator.serviceWorker.register( + 'sw.js', + { + scope: '/', + } + ); + if (registration.installing) { + console.log('Service worker installing'); + } else if (registration.waiting) { + console.log('Service worker installed'); + } else if (registration.active) { + console.log('Service worker active'); + } + } catch (error) { + console.error(`Registration failed with ${error}`); + } + } +}; + +registerServiceWorker(); \ No newline at end of file diff --git a/src/sw.js b/src/sw.js index 4ccdcb7..aa5f7da 100644 --- a/src/sw.js +++ b/src/sw.js @@ -2,7 +2,18 @@ importScripts( 'https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js' ); -workbox.routing.registerRoute( - ({ request }) => request.destination === 'image', - new workbox.strategies.CacheFirst() -); \ No newline at end of file +const navigationRoute = new workbox.routing.NavigationRoute( + new workbox.strategies.NetworkFirst({ + cacheName: 'apexie-navigations' + }) +); + +const imageAssetRoute = new workbox.routing.Route(({request}) => { + return request.destination === 'image'; + }, new workbox.strategies.CacheFirst({ + cacheName: 'apexie-image-assets' + }) +); + +workbox.routing.registerRoute(navigationRoute); +workbox.routing.registerRoute(imageAssetRoute); \ No newline at end of file