|
| 1 | +import { default as params } from '@params' |
| 2 | +import { CacheableResponsePlugin } from 'workbox-cacheable-response' |
| 3 | +import { ExpirationPlugin } from 'workbox-expiration' |
| 4 | +import { registerRoute, setCatchHandler } from 'workbox-routing' |
| 5 | +import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies' |
| 6 | + |
| 7 | +self.__WB_DISABLE_DEV_LOGS = !params.debug |
| 8 | + |
| 9 | +const debug = (...data: any[]): void => { |
| 10 | + if (self.__WB_DISABLE_DEV_LOGS) { |
| 11 | + return |
| 12 | + } |
| 13 | + |
| 14 | + console.debug('[pwa]', ...data); |
| 15 | +} |
| 16 | + |
| 17 | +const cachePrefix = 'hugo-pwa-' |
| 18 | +const fallbacksCache = cachePrefix + 'fallbacks' |
| 19 | +// Filter the invalid URLs, such as temporary URLs generated by Hugo PostProgress. |
| 20 | +const precaches = params.precaches.filter((url) => url.indexOf('__h_pp_l1') !== 0) |
| 21 | + |
| 22 | +// Register page route with NetworkFirst strategy. |
| 23 | +// There will be a problem with CacheFirst or StaleWhileRevalidate strategy |
| 24 | +// if the cached page loads no longer exist or expired assets, such as CSS and JS. |
| 25 | +registerRoute( |
| 26 | + ({ request }) => { |
| 27 | + return request.mode === 'navigate'; |
| 28 | + }, |
| 29 | + new NetworkFirst({ |
| 30 | + cacheName: cachePrefix + 'pages', |
| 31 | + plugins: [ |
| 32 | + new CacheableResponsePlugin({ |
| 33 | + statuses: [200], |
| 34 | + }), |
| 35 | + ], |
| 36 | + }) |
| 37 | +) |
| 38 | + |
| 39 | +// Register assets routes. |
| 40 | +const assets = ['font', 'image', 'script', 'style'] |
| 41 | +for (let i in assets) { |
| 42 | + const kind = assets[i] |
| 43 | + const cache = params.caches[kind] |
| 44 | + const cacheName = cachePrefix + kind + 's' |
| 45 | + let strategy = null |
| 46 | + let plugins = [ |
| 47 | + new CacheableResponsePlugin({ |
| 48 | + statuses: [200], |
| 49 | + }), |
| 50 | + new ExpirationPlugin({ |
| 51 | + maxAgeSeconds: cache.max_age ?? 60 * 60 * 24 * 30, |
| 52 | + }) |
| 53 | + ] |
| 54 | + switch (cache.strategy) { |
| 55 | + case 'network-first': |
| 56 | + strategy = new NetworkFirst({ |
| 57 | + cacheName: cacheName, |
| 58 | + plugins: plugins, |
| 59 | + }) |
| 60 | + break |
| 61 | + case 'cache-first': |
| 62 | + strategy = new CacheFirst({ |
| 63 | + cacheName: cacheName, |
| 64 | + plugins: plugins, |
| 65 | + }) |
| 66 | + break |
| 67 | + case 'stale-while-revalidate': |
| 68 | + strategy = new StaleWhileRevalidate({ |
| 69 | + cacheName: cacheName, |
| 70 | + plugins: plugins, |
| 71 | + }) |
| 72 | + break |
| 73 | + default: |
| 74 | + throw new Error(`invalid strategy for kind "${kind}": ` + cache.strategy) |
| 75 | + } |
| 76 | + registerRoute( |
| 77 | + ({ request }) => { |
| 78 | + return request.destination === kind; |
| 79 | + }, |
| 80 | + strategy |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +self.addEventListener('install', event => { |
| 85 | + event.waitUntil( |
| 86 | + self.caches |
| 87 | + .open(fallbacksCache) |
| 88 | + .then(cache => cache.addAll(precaches)) |
| 89 | + ); |
| 90 | +}); |
| 91 | + |
| 92 | +const handler = async options => { |
| 93 | + debug('catch handler', options.request) |
| 94 | + const dest = options.request.destination |
| 95 | + const url = options.request.url |
| 96 | + const cache = await self.caches.open(fallbacksCache) |
| 97 | + |
| 98 | + // Return the cached item if found. |
| 99 | + const cached = await cache.match(url) |
| 100 | + if (cached) { |
| 101 | + return cached |
| 102 | + } |
| 103 | + |
| 104 | + if (dest === 'document') { |
| 105 | + const offline = '/offline.html' |
| 106 | + let lang = '' |
| 107 | + let paths: string[] |
| 108 | + if (url.indexOf(params.baseURL) === 0) { |
| 109 | + paths = url.replace(params.baseURL, '').split('/', 1) |
| 110 | + } else { |
| 111 | + paths = (new URL(url)).pathname.replace(/^\//, '').split('/', 1) |
| 112 | + } |
| 113 | + if (paths.length > 0 && params.langs.includes(paths[0])) { |
| 114 | + lang = paths[0] |
| 115 | + } |
| 116 | + |
| 117 | + debug('loading offline page', `/${lang}/offline.html`, offline) |
| 118 | + return (await cache.match(`/${lang}/offline.html`)) |
| 119 | + || (await cache.match(offline)) |
| 120 | + || Response.error() |
| 121 | + } else if (dest === 'image' && params.offline_image) { |
| 122 | + return (await cache.match(params.offline_image)) |
| 123 | + || Response.error() |
| 124 | + } |
| 125 | + |
| 126 | + // Return a error response. |
| 127 | + return Response.error() |
| 128 | +}; |
| 129 | + |
| 130 | +setCatchHandler(handler) |
0 commit comments