service-worker.js 1.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/* globals workbox */
/* eslint-disable no-restricted-globals */
workbox.core.setCacheNameDetails({
  prefix: 'antd-pro',
  suffix: 'v1',
});
// Control all opened tabs ASAP
workbox.clientsClaim();

/**
 * Use precaching list generated by workbox in build process.
 * https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.precaching
 */
/* eslint-disable no-underscore-dangle */
workbox.precaching.precacheAndRoute(self.__precacheManifest || []);

/**
 * Register a navigation route.
 * https://developers.google.com/web/tools/workbox/modules/workbox-routing#how_to_register_a_navigation_route
 */
workbox.routing.registerNavigationRoute('/index.html');

/**
 * Use runtime cache:
 * https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.routing#.registerRoute
 *
 * Workbox provides all common caching strategies including CacheFirst, NetworkFirst etc.
 * https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.strategies
 */

/**
 * Handle API requests
 */
workbox.routing.registerRoute(/\/api\//, workbox.strategies.networkFirst());

/**
 * Handle third party requests
 */
workbox.routing.registerRoute(
  /^https:\/\/gw.alipayobjects.com\//,
何乐's avatar
何乐 committed
41
  workbox.strategies.networkFirst(),
42 43 44
);
workbox.routing.registerRoute(
  /^https:\/\/cdnjs.cloudflare.com\//,
何乐's avatar
何乐 committed
45
  workbox.strategies.networkFirst(),
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
);
workbox.routing.registerRoute(/\/color.less/, workbox.strategies.networkFirst());

/**
 * Response to client after skipping waiting with MessageChannel
 */
addEventListener('message', event => {
  const replyPort = event.ports[0];
  const message = event.data;
  if (replyPort && message && message.type === 'skip-waiting') {
    event.waitUntil(
      self
        .skipWaiting()
        .then(
          () => replyPort.postMessage({ error: null }),
何乐's avatar
何乐 committed
61 62
          error => replyPort.postMessage({ error }),
        ),
63 64 65
    );
  }
});