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