1/** 2Import the Workbox library from the Google CDN. 3It is possible to serve this locally should we want to remove the dependency on Google 4See here for more info: https://developers.google.com/web/tools/workbox/modules/workbox-sw 5**/ 6importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.3.1/workbox-sw.js'); 7const offlinePage = '/offline/'; 8// SETTINGS 9workbox.setConfig({ 10 // set the local path is not using Google CDN 11 //modulePathPrefix: '/directory/to/workbox/' 12 13 // By default, workbox-sw will use the debug build for sites on localhost, 14 // but for any other origin itâll use the production build. Force by setting to true. 15 // debug: true 16}); 17 18/** 19 * Pages to cache (networkFirst) 20 */ 21 22/** 23 * Add on install 24 */ 25self.addEventListener('install', (event) => { 26 const urls = ['/offline/']; 27 const cacheName = workbox.core.cacheNames.runtime; 28 event.waitUntil(caches.open(cacheName).then((cache) => cache.addAll(urls))) 29}); 30 31var networkFirst = workbox.strategies.networkFirst({ 32 cacheName: 'cache-pages', 33 plugins: [ 34 { 35 cacheWillUpdate: async ({response}) => { 36 return response && response.status === 200 ? response : null; 37 } 38 } 39 ] 40}); 41 42const customHandler = async (args) => { 43 try { 44 const response = await networkFirst.handle(args); 45 return response || await caches.match(offlinePage); 46 } catch (error) { 47 return await caches.match(offlinePage); 48 } 49}; 50 51workbox.routing.registerRoute( 52 new RegExp('/'), 53 customHandler 54); 55 56// PATCH: Media files cache with 200 responses only (avoid 206 errors) 57workbox.routing.registerRoute( 58 ({url, event}) => 59 url.origin.includes('s3.amazonaws.com') || // adjust to your actual CDN/origin 60 url.origin.includes('media.c-path.org') || // add more as needed 61 url.pathname.match(/\.(?:png|jpg|jpeg|gif|mp4|webm|mp3|ico|ogg)$/), 62 workbox.strategies.cacheFirst({ 63 cacheName: 'media-cache', 64 plugins: [ 65 { 66 cacheWillUpdate: async ({response}) => { 67 // Only cache 200 OK responses to avoid partial content (206) errors 68 return response && response.status === 200 ? response : null; 69 } 70 } 71 ] 72 }) 73); 74//-------------------------------------------------------------------- 75 76// If any precache rules are defined in the Workbox setup this is where they will be injected 77workbox.precaching.precacheAndRoute([]); 78 79// skip the 'worker in waiting' phase and activate immediately 80workbox.skipWaiting(); 81// claim any clients that match the worker scope immediately. requests on these pages will 82// now go via the service worker. 83workbox.clientsClaim();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.