service-worker.js 3.9 KB

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