importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.3.0/workbox-sw.js'); //{clientsClaim: true} //console.log('this is my custom service worker'); const revision = 'version-1'; const longerRevision = 'static-1'; //Config workbox.setConfig({ debug: true }); // Offline Google Analytics workbox.googleAnalytics.initialize(); // Google fonts workbox.routing.registerRoute( new RegExp('https://fonts.(?:googleapis|gstatic).com/(.*)'), workbox.strategies.cacheFirst({ cacheName: 'googleapis', revision: longerRevision, plugins: [ new workbox.expiration.Plugin({ maxEntries: 30, }), ], }), ); //Static routes workbox.routing.registerRoute( '/', workbox.strategies.networkFirst({ cacheName: 'network-first', revision: revision, plugins: [ new workbox.expiration.Plugin({ // Cache for a maximum of a week maxAgeSeconds: 7 * 24 * 60 * 60, }) ], }) ); workbox.routing.registerRoute( /\.(?:png|gif|jpg|jpeg|svg)$/, workbox.strategies.cacheFirst({ cacheName: 'images', revision: revision, plugins: [ new workbox.expiration.Plugin({ maxEntries: 60, maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days }), ], }), ); workbox.routing.registerRoute( /.*(vendors|bundle).(css|js).*$/, workbox.strategies.staleWhileRevalidate({ cacheName: 'static-resources', revision: revision, plugins: [ new workbox.expiration.Plugin({ // Cache only 20 images maxEntries: 20, // Cache for a maximum of a week maxAgeSeconds: 7 * 24 * 60 * 60, }) ], }) ); workbox.routing.registerRoute( '/offline', workbox.strategies.cacheFirst({ cacheName: 'offline-page', revision: revision, }) ); const pageHandler = workbox.strategies.networkFirst({ cacheName: 'dynamic', networkTimetoutSeconds: 5, plugins: [ new workbox.expiration.Plugin({ maxEntries: 50, }) ] }); workbox.routing.registerRoute(function (routeData) { return routeData.event.request.headers.get('accept').includes('text/html'); }, args => { return pageHandler.handle(args).then(response => { if (!response) { return caches.match('/offline'); } // else if (response.status === 404) { // return caches.match('pages/404.html'); // } return response; }); }); //other stuff workbox.precaching.precacheAndRoute([]);