No Description

service-worker.ts 2.9KB

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