|
1 |
| - |
2 |
| -if (location.href.includes('howdz.xyz')) { |
3 |
| - importScripts('https://cdn.staticfile.org/workbox-sw/7.0.0/workbox-sw.js') |
4 |
| - workbox.setConfig({ |
5 |
| - debug: false, |
6 |
| - }); |
7 |
| - console.log('sw.js is load by CDN!') |
8 |
| -} else { |
9 |
| - importScripts('./workbox/workbox-sw.js') |
10 |
| - workbox.setConfig({ |
11 |
| - debug: false, |
12 |
| - modulePathPrefix: './workbox/' |
13 |
| - }); |
14 |
| - console.log('sw.js is load by local!') |
15 |
| -} |
16 |
| - |
17 |
| -// Cache css/js/font. |
18 |
| -workbox.routing.registerRoute( |
19 |
| - ({ request }) => request.destination === 'style' || request.destination === 'script' || request.destination === 'font', |
20 |
| - new workbox.strategies.CacheFirst({ |
21 |
| - cacheName: 'css-js-font', |
22 |
| - plugins: [ |
23 |
| - new workbox.cacheableResponse.CacheableResponsePlugin({ |
24 |
| - statuses: [200], |
25 |
| - }), |
26 |
| - new workbox.expiration.ExpirationPlugin({ |
27 |
| - maxEntries: 50, |
28 |
| - maxAgeSeconds: 60 * 60 * 24 * 7, // 7 Days |
29 |
| - }), |
30 |
| - ] |
31 |
| - }) |
32 |
| -); |
33 |
| - |
34 |
| -// Cache image. |
35 |
| -workbox.routing.registerRoute( |
36 |
| - ({ request }) => request.destination === 'image', |
37 |
| - new workbox.strategies.StaleWhileRevalidate({ |
38 |
| - cacheName: 'image', |
39 |
| - plugins: [ |
40 |
| - new workbox.cacheableResponse.CacheableResponsePlugin({ |
41 |
| - statuses: [200], |
42 |
| - }), |
43 |
| - new workbox.expiration.ExpirationPlugin({ |
44 |
| - maxEntries: 50, |
45 |
| - maxAgeSeconds: 60 * 60 * 24 * 7, // 7 Days |
46 |
| - }) |
47 |
| - ] |
48 |
| - }) |
49 |
| -) |
50 |
| - |
51 |
| -// Cache video |
52 |
| -workbox.routing.registerRoute( |
53 |
| - ({ request }) => request.destination === 'video', |
54 |
| - new workbox.strategies.CacheFirst({ |
55 |
| - cacheName: 'video', |
56 |
| - plugins: [ |
57 |
| - new workbox.cacheableResponse.CacheableResponsePlugin({ |
58 |
| - statuses: [200], |
59 |
| - }), |
60 |
| - new workbox.expiration.ExpirationPlugin({ |
61 |
| - maxEntries: 50, |
62 |
| - maxAgeSeconds: 60 * 60 * 24 * 7, // 7 Days |
63 |
| - }), |
64 |
| - new workbox.rangeRequests.RangeRequestsPlugin() |
65 |
| - ] |
66 |
| - }) |
67 |
| -) |
| 1 | + |
| 2 | +if (location.href.includes('howdz.xyz')) { |
| 3 | + importScripts('https://cdn.staticfile.org/workbox-sw/7.0.0/workbox-sw.js') |
| 4 | + workbox.setConfig({ |
| 5 | + debug: false, |
| 6 | + }); |
| 7 | + console.log('sw.js is load by CDN!') |
| 8 | +} else { |
| 9 | + importScripts('./workbox/workbox-sw.js') |
| 10 | + workbox.setConfig({ |
| 11 | + debug: false, |
| 12 | + modulePathPrefix: './workbox/' |
| 13 | + }); |
| 14 | + console.log('sw.js is load by local!') |
| 15 | +} |
| 16 | + |
| 17 | +// Cache css/js/font. |
| 18 | +workbox.routing.registerRoute( |
| 19 | + /** |
| 20 | + * Checks if the request destination is either 'style', 'script' or 'font'. |
| 21 | + * @param {{request: Object}} object - An object containing a request property |
| 22 | + * @returns {Boolean} True if the request destination matches 'style', 'script', or 'font'. Otherwise, returns false. |
| 23 | + */ |
| 24 | + ({ request }) => request.destination === 'style' || request.destination === 'script' || request.destination === 'font', |
| 25 | + new workbox.strategies.CacheFirst({ |
| 26 | + cacheName: 'css-js-font', |
| 27 | + plugins: [ |
| 28 | + new workbox.cacheableResponse.CacheableResponsePlugin({ |
| 29 | + statuses: [200], |
| 30 | + }), |
| 31 | + new workbox.expiration.ExpirationPlugin({ |
| 32 | + maxEntries: 50, |
| 33 | + maxAgeSeconds: 60 * 60 * 24 * 7, // 7 Days |
| 34 | + }), |
| 35 | + ] |
| 36 | + }) |
| 37 | +); |
| 38 | + |
| 39 | +// Cache image. |
| 40 | +workbox.routing.registerRoute( |
| 41 | + /** |
| 42 | + * A method checking the destination property of the request object. |
| 43 | + * @param {Object} request - An object containing request properties. |
| 44 | + * @returns {Boolean} Returns true if request destination is 'image', false otherwise. |
| 45 | + */ |
| 46 | + ({ request }) => request.destination === 'image', |
| 47 | + new workbox.strategies.StaleWhileRevalidate({ |
| 48 | + cacheName: 'image', |
| 49 | + plugins: [ |
| 50 | + new workbox.cacheableResponse.CacheableResponsePlugin({ |
| 51 | + statuses: [200], |
| 52 | + }), |
| 53 | + new workbox.expiration.ExpirationPlugin({ |
| 54 | + maxEntries: 50, |
| 55 | + maxAgeSeconds: 60 * 60 * 24 * 7, // 7 Days |
| 56 | + }) |
| 57 | + ] |
| 58 | + }) |
| 59 | +) |
| 60 | + |
| 61 | +// Cache video |
| 62 | +workbox.routing.registerRoute( |
| 63 | + /** |
| 64 | + * This method checks if the request's destination is a video. |
| 65 | + * @param {Object} request - The object that contains a destination property. |
| 66 | + * @returns {Boolean} Returns true if the destination of the request is 'video', false otherwise. |
| 67 | + */ |
| 68 | + ({ request }) => request.destination === 'video', |
| 69 | + new workbox.strategies.CacheFirst({ |
| 70 | + cacheName: 'video', |
| 71 | + plugins: [ |
| 72 | + new workbox.cacheableResponse.CacheableResponsePlugin({ |
| 73 | + statuses: [200], |
| 74 | + }), |
| 75 | + new workbox.expiration.ExpirationPlugin({ |
| 76 | + maxEntries: 50, |
| 77 | + maxAgeSeconds: 60 * 60 * 24 * 7, // 7 Days |
| 78 | + }), |
| 79 | + new workbox.rangeRequests.RangeRequestsPlugin() |
| 80 | + ] |
| 81 | + }) |
| 82 | +) |
0 commit comments