service-worker.js
1 /* eslint-disable no-restricted-globals */ 2 3 // This service worker can be customized! 4 // See https://developers.google.com/web/tools/workbox/modules 5 // for the list of available Workbox modules, or add any other 6 // code you'd like. 7 // You can also remove this file if you'd prefer not to use a 8 // service worker, and the Workbox build step will be skipped. 9 10 import { clientsClaim } from 'workbox-core'; 11 import { ExpirationPlugin } from 'workbox-expiration'; 12 import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching'; 13 import { registerRoute } from 'workbox-routing'; 14 import { StaleWhileRevalidate } from 'workbox-strategies'; 15 16 clientsClaim(); 17 18 // fix index.html not refreshing on new plebbit-react versions 19 registerRoute( 20 // match index.html 21 ({ url }) => url.pathname === '/', 22 // serve cached index.html first but revalidate in background 23 new StaleWhileRevalidate(), 24 ); 25 26 // Precache all of the assets generated by your build process. 27 // Their URLs are injected into the manifest variable below. 28 // This variable must be present somewhere in your service worker file, 29 // even if you decide not to use precaching. See https://cra.link/PWA 30 precacheAndRoute(self.__WB_MANIFEST); 31 32 // Set up App Shell-style routing, so that all navigation requests 33 // are fulfilled with your index.html shell. Learn more at 34 // https://developers.google.com/web/fundamentals/architecture/app-shell 35 const fileExtensionRegexp = new RegExp('/[^/?]+\\.[^/]+$'); 36 registerRoute( 37 // Return false to exempt requests from being fulfilled by index.html. 38 ({ request, url }) => { 39 // If this isn't a navigation, skip. 40 if (request.mode !== 'navigate') { 41 return false; 42 } // If this is a URL that starts with /_, skip. 43 44 if (url.pathname.startsWith('/_')) { 45 return false; 46 } // If this looks like a URL for a resource, because it contains // a file extension, skip. 47 48 if (url.pathname.match(fileExtensionRegexp)) { 49 return false; 50 } // Return true to signal that we want to use the handler. 51 52 return true; 53 }, 54 createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html'), 55 ); 56 57 // An example runtime caching route for requests that aren't handled by the 58 // precache, in this case same-origin .png requests like those from in public/ 59 registerRoute( 60 // Add in any other file extensions or routing criteria as needed. 61 ({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.png'), // Customize this strategy as needed, e.g., by changing to CacheFirst. 62 new StaleWhileRevalidate({ 63 cacheName: 'images', 64 plugins: [ 65 // Ensure that once this runtime cache reaches a maximum size the 66 // least-recently used images are removed. 67 new ExpirationPlugin({ maxEntries: 50 }), 68 ], 69 }), 70 ); 71 72 // This allows the web app to trigger skipWaiting via 73 // registration.waiting.postMessage({type: 'SKIP_WAITING'}) 74 self.addEventListener('message', (event) => { 75 if (event.data && event.data.type === 'SKIP_WAITING') { 76 self.skipWaiting(); 77 } 78 }); 79 80 // Any other custom service worker logic can go here.