service-worker.js 2.8 KB

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