|
1 |
| -// THIS FILE SHOULD NOT BE VERSION CONTROLLED |
| 1 | +const options = {"workboxURL":"https://cdn.jsdelivr.net/npm/workbox-cdn@5.1.3/workbox/workbox-sw.js","importScripts":[],"config":{"debug":false},"clientsClaim":true,"skipWaiting":true,"cleanupOutdatedCaches":true,"offlineAnalytics":false,"preCaching":["/vue-api-query/?standalone=true"],"runtimeCaching":[{"urlPattern":"/vue-api-query/_nuxt/","handler":"CacheFirst","method":"GET","strategyPlugins":[]},{"urlPattern":"/vue-api-query/","handler":"NetworkFirst","method":"GET","strategyPlugins":[]}],"offlinePage":null,"pagesURLPattern":"/vue-api-query/","offlineStrategy":"NetworkFirst"} |
2 | 2 |
|
3 |
| -// https://github.com/NekR/self-destroying-sw |
| 3 | +importScripts(...[options.workboxURL, ...options.importScripts]) |
4 | 4 |
|
5 |
| -self.addEventListener('install', function (e) { |
6 |
| - self.skipWaiting() |
7 |
| -}) |
| 5 | +initWorkbox(workbox, options) |
| 6 | +workboxExtensions(workbox, options) |
| 7 | +precacheAssets(workbox, options) |
| 8 | +cachingExtensions(workbox, options) |
| 9 | +runtimeCaching(workbox, options) |
| 10 | +offlinePage(workbox, options) |
| 11 | +routingExtensions(workbox, options) |
8 | 12 |
|
9 |
| -self.addEventListener('activate', function (e) { |
10 |
| - self.registration.unregister() |
11 |
| - .then(function () { |
12 |
| - return self.clients.matchAll() |
13 |
| - }) |
14 |
| - .then(function (clients) { |
15 |
| - clients.forEach(client => client.navigate(client.url)) |
| 13 | +function getProp(obj, prop) { |
| 14 | + return prop.split('.').reduce((p, c) => p[c], obj) |
| 15 | +} |
| 16 | + |
| 17 | +function initWorkbox(workbox, options) { |
| 18 | + if (options.config) { |
| 19 | + // Set workbox config |
| 20 | + workbox.setConfig(options.config) |
| 21 | + } |
| 22 | + |
| 23 | + if (options.cacheNames) { |
| 24 | + // Set workbox cache names |
| 25 | + workbox.core.setCacheNameDetails(options.cacheNames) |
| 26 | + } |
| 27 | + |
| 28 | + if (options.clientsClaim) { |
| 29 | + // Start controlling any existing clients as soon as it activates |
| 30 | + workbox.core.clientsClaim() |
| 31 | + } |
| 32 | + |
| 33 | + if (options.skipWaiting) { |
| 34 | + workbox.core.skipWaiting() |
| 35 | + } |
| 36 | + |
| 37 | + if (options.cleanupOutdatedCaches) { |
| 38 | + workbox.precaching.cleanupOutdatedCaches() |
| 39 | + } |
| 40 | + |
| 41 | + if (options.offlineAnalytics) { |
| 42 | + // Enable offline Google Analytics tracking |
| 43 | + workbox.googleAnalytics.initialize() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function precacheAssets(workbox, options) { |
| 48 | + if (options.preCaching.length) { |
| 49 | + workbox.precaching.precacheAndRoute(options.preCaching, options.cacheOptions) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function runtimeCaching(workbox, options) { |
| 54 | + for (const entry of options.runtimeCaching) { |
| 55 | + const urlPattern = new RegExp(entry.urlPattern) |
| 56 | + const method = entry.method || 'GET' |
| 57 | + |
| 58 | + const plugins = (entry.strategyPlugins || []) |
| 59 | + .map(p => new (getProp(workbox, p.use))(...p.config)) |
| 60 | + |
| 61 | + const strategyOptions = { ...entry.strategyOptions, plugins } |
| 62 | + |
| 63 | + const strategy = new workbox.strategies[entry.handler](strategyOptions) |
| 64 | + |
| 65 | + workbox.routing.registerRoute(urlPattern, strategy, method) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function offlinePage(workbox, options) { |
| 70 | + if (options.offlinePage) { |
| 71 | + // Register router handler for offlinePage |
| 72 | + workbox.routing.registerRoute(new RegExp(options.pagesURLPattern), ({ request, event }) => { |
| 73 | + const strategy = new workbox.strategies[options.offlineStrategy] |
| 74 | + return strategy |
| 75 | + .handle({ request, event }) |
| 76 | + .catch(() => caches.match(options.offlinePage)) |
16 | 77 | })
|
17 |
| -}) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function workboxExtensions(workbox, options) { |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +function cachingExtensions(workbox, options) { |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +function routingExtensions(workbox, options) { |
| 90 | + |
| 91 | +} |
0 commit comments