|
| 1 | +const { assign } = Object; |
| 2 | + |
| 3 | +// Wipe all caches for this domain. |
| 4 | +const clear = ({ quiet }) => caches.keys().then(cachesNames => { |
| 5 | + return Promise.all(cachesNames.map(cacheName => { |
| 6 | + return caches.delete(cacheName).then(function () { |
| 7 | + if (!quiet) { |
| 8 | + console.warn(`diffHTML ServiceWorker: Deleted cache ${cacheName}`); |
| 9 | + } |
| 10 | + }); |
| 11 | + })) |
| 12 | +}); |
| 13 | + |
| 14 | +// Wipe out all the service workers for this domain. |
| 15 | +const unregister = ({ quiet }) => navigator.serviceWorker.getRegistrations().then(regs => { |
| 16 | + for (let reg of regs) { |
| 17 | + reg.unregister(); |
| 18 | + |
| 19 | + if (!quiet) { |
| 20 | + console.warn('diffHTML ServiceWorker: Unregistering worker', reg); |
| 21 | + } |
| 22 | + } |
| 23 | +}); |
| 24 | + |
| 25 | +// Simplify's creating a service worker with diffHTML. |
| 26 | +const wrapper = ({ |
| 27 | + // Default to the root /service-worker.js. |
| 28 | + url = '/service-worker.js', |
| 29 | + |
| 30 | + // Allow the middleware to automatically clear all caches. By default this is |
| 31 | + // disabled as it defeats the point of a service worker and may muck with |
| 32 | + // other apps running on localhost. |
| 33 | + autoClearCaches = location.search.includes('diff_autoclear'), |
| 34 | + |
| 35 | + // Useful for debugging, wipes and doesn't set. Default to the query param |
| 36 | + // for disable. |
| 37 | + disable = location.search.includes('diff_disable'), |
| 38 | + |
| 39 | + // Useful for debugging, logs out service worker events. Default to the query |
| 40 | + // param to enable. |
| 41 | + quiet = location.search.includes('diff_quiet'), |
| 42 | + |
| 43 | + // Defaults to the page root, gets set into the service worker options. |
| 44 | + scope = '/', |
| 45 | + |
| 46 | + // The remaining options to be spread into the serviceWorker configuration. |
| 47 | + options = {}, |
| 48 | +}) => assign(function serviceWorkerMiddleware() {}, { |
| 49 | + subscribe() { |
| 50 | + const { serviceWorker } = navigator; |
| 51 | + |
| 52 | + let chain = Promise.resolve(); |
| 53 | + |
| 54 | + // If the user wants to work on the service worker, we need to clear the |
| 55 | + // old ones out first. |
| 56 | + if (autoClearCaches) { |
| 57 | + chain = chain.then(() => clear({ quiet })); |
| 58 | + } |
| 59 | + |
| 60 | + // If we're disabling, then clear the c |
| 61 | + if (disable) { |
| 62 | + // Iterate through all service workers and remove the old ones. |
| 63 | + chain = chain.then(() => unregister({ quiet })); |
| 64 | + } |
| 65 | + |
| 66 | + // If the user disables the service worker, do not attempt to re-register |
| 67 | + // it. |
| 68 | + chain = chain.then(() => { |
| 69 | + if (!disable) { |
| 70 | + return serviceWorker.register(url, { scope, ...options }) |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + // If not in quiet mode, echo out some standard messaging and give access |
| 75 | + // to the objects. |
| 76 | + if (!quiet) { |
| 77 | + chain |
| 78 | + .then(reg => reg && console.warn( |
| 79 | + 'diffHTML ServiceWorker: Registration succeeded', reg |
| 80 | + )) |
| 81 | + .catch(err => err && console.warn( |
| 82 | + 'diffHTML ServiceWorker: Registration failed', err |
| 83 | + )); |
| 84 | + } |
| 85 | + }, |
| 86 | + |
| 87 | + unsubscribe() { |
| 88 | + unregister({ quiet }); |
| 89 | + }, |
| 90 | +}); |
| 91 | + |
| 92 | +export default opts => wrapper(opts || {}); |
0 commit comments