service-worker.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* globals workbox */
  2. /* eslint-disable no-restricted-globals */
  3. workbox.core.setCacheNameDetails({
  4. prefix: 'antd-pro',
  5. suffix: 'v1',
  6. });
  7. // Control all opened tabs ASAP
  8. workbox.clientsClaim();
  9. /**
  10. * Use precaching list generated by workbox in build process.
  11. * https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.precaching
  12. */
  13. /* eslint-disable no-underscore-dangle */
  14. workbox.precaching.precacheAndRoute(self.__precacheManifest || []);
  15. /**
  16. * Register a navigation route.
  17. * https://developers.google.com/web/tools/workbox/modules/workbox-routing#how_to_register_a_navigation_route
  18. */
  19. workbox.routing.registerNavigationRoute('/index.html');
  20. /**
  21. * Use runtime cache:
  22. * https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.routing#.registerRoute
  23. *
  24. * Workbox provides all common caching strategies including CacheFirst, NetworkFirst etc.
  25. * https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.strategies
  26. */
  27. /**
  28. * Handle API requests
  29. */
  30. workbox.routing.registerRoute(/\/api\//, workbox.strategies.networkFirst());
  31. /**
  32. * Handle third party requests
  33. */
  34. workbox.routing.registerRoute(
  35. /^https:\/\/gw.alipayobjects.com\//,
  36. workbox.strategies.networkFirst()
  37. );
  38. workbox.routing.registerRoute(
  39. /^https:\/\/cdnjs.cloudflare.com\//,
  40. workbox.strategies.networkFirst()
  41. );
  42. workbox.routing.registerRoute(/\/color.less/, workbox.strategies.networkFirst());
  43. /**
  44. * Response to client after skipping waiting with MessageChannel
  45. */
  46. addEventListener('message', event => {
  47. const replyPort = event.ports[0];
  48. const message = event.data;
  49. if (replyPort && message && message.type === 'skip-waiting') {
  50. event.waitUntil(
  51. self
  52. .skipWaiting()
  53. .then(
  54. () => replyPort.postMessage({ error: null }),
  55. error => replyPort.postMessage({ error })
  56. )
  57. );
  58. }
  59. });