|
1 |
| -import { Workbox } from 'workbox-window' |
| 1 | +/* eslint-disable no-console */ |
| 2 | +/** |
| 3 | + * See workbox documentation for details about the lifecyle events of a service worker. |
| 4 | + * https://developers.google.com/web/tools/workbox/modules/workbox-window#important_service_worker_lifecycle_moments |
| 5 | + */ |
| 6 | + import { Workbox } from 'workbox-window' |
2 | 7 |
|
3 |
| -// const autoUpdate = async (registration: ServiceWorkerRegistration | undefined) => { |
4 |
| -// setInterval(async () => { |
5 |
| -// try { |
6 |
| -// /* eslint-disable-next-line no-unused-expressions */ |
7 |
| -// await registration?.update() |
8 |
| -// } catch (err) { |
9 |
| -// /* eslint-disable-next-line no-console */ |
10 |
| -// console.log(`sw: registration autoUpdate failed: ${err}`) |
11 |
| -// } |
12 |
| -// }, 1000 * 60 * 60) |
13 |
| -// } |
| 8 | + const autoUpdate = async (registration) => { |
| 9 | + // const updateInterval = 1000 * 60 * 60 // 1 hour |
| 10 | + const updateInterval = 100 * 60 * 60 // 1 min // for debugging |
| 11 | + setInterval(async () => { |
| 12 | + try { |
| 13 | + /* eslint-disable-next-line no-unused-expressions */ |
| 14 | + await registration?.update() |
| 15 | + console.log('sw: registration checked for updates') |
| 16 | + } catch (err) { |
| 17 | + console.log(`sw: registration failed to check for updates with errors: ${err}`) |
| 18 | + } |
| 19 | + }, updateInterval) |
| 20 | + } |
14 | 21 |
|
15 |
| -// const manualUpdateAvailable = (registration: ServiceWorkerRegistration | undefined) => { |
16 |
| -// // Wires up an event that we can listen to in the app. Example: listen for available update and prompt user to update. |
17 |
| -// document.dispatchEvent( |
18 |
| -// new CustomEvent('swUpdated', { detail: registration })) |
19 |
| -// } |
| 22 | + const manualUpdateAvailable = (registration) => { |
| 23 | + // Wires up an event that we can listen to in the app. Example: listen for available update and prompt user to update. |
| 24 | + document.dispatchEvent( |
| 25 | + new CustomEvent('swUpdated', { detail: registration })) |
| 26 | + } |
20 | 27 |
|
21 |
| -const register = async () => { |
22 |
| - if ('serviceWorker' in navigator) { |
23 |
| - // Workbox combines the ./src/sw.js file and injected manifest into the servicer-worker.js file in /dist |
24 |
| - // Uses vue.config.js workboxOptions.swSrc for the location of sw.js and swDest for the output location of 'service-worker.js'. |
25 |
| - // You can override the file names and locations by changing the values of workboxOptions in vue.config.js. |
26 |
| - const wb = new Workbox(`${process.env.BASE_URL}service-worker.js`) |
| 28 | + const register = async () => { |
| 29 | + if ('serviceWorker' in navigator) { |
| 30 | + // Workbox combines the ./src/sw.js file and injected manifest into the servicer-worker.js file in /dist |
| 31 | + // Uses vue.config.js workboxOptions.swSrc for the location of sw.js and swDest for the output location of 'service-worker.js'. |
| 32 | + // You can override the file names and locations by changing the values of workboxOptions in vue.config.js. |
| 33 | + const wb = new Workbox(`${process.env.BASE_URL}service-worker.js`) |
27 | 34 |
|
28 |
| - // wire up instance of registration so we can take further action on it. |
29 |
| - const registration = await wb.register() |
30 |
| - console.log(`sw: waiting: ${registration.waiting}`) |
| 35 | + // wire up instance of registration so we can take further action on it. |
| 36 | + const registration = await wb.register() |
| 37 | + console.log('sw: registered') |
31 | 38 |
|
32 |
| - // autoUpdate(registration) |
| 39 | + autoUpdate(registration) |
33 | 40 |
|
34 |
| - // wb.addEventListener('activated', async (event) => { |
35 |
| - // if (event.isUpdate) { |
36 |
| - // // event.isUpdate=true means the service worker was already registered and there is a new version available. |
| 41 | + wb.addEventListener('activated', async (event) => { |
| 42 | + // if (event.isUpdate) { |
| 43 | + // event.isUpdate=true means the service worker was already registered and there is a new version available. |
37 | 44 |
|
38 |
| - // // this only triggers self.skipWaiting. It still doesn't force the app to update. See /composables/use-service-worker.ts for updating app. |
39 |
| - // wb.messageSkipWaiting() |
| 45 | + // this only triggers self.skipWaiting. It still doesn't force the app to update. See /composables/use-service-worker.ts for updating app. |
| 46 | + wb.messageSkipWaiting() |
| 47 | + // } else { |
| 48 | + // // first time use when event.isUpdate = false |
| 49 | + // // service worker should claim the client immediately since its the first install. |
| 50 | + // wb.messageSW({ type: 'CLIENTS_CLAIM' }) |
| 51 | + // console.log('sw: clientsClaim called.') |
| 52 | + // } |
| 53 | + }) |
40 | 54 |
|
41 |
| - // // Wires up an event that we can listen to in the app for manual updates. Example: listen for available update and prompt user to update. |
42 |
| - // manualUpdateAvailable(registration) |
43 |
| - // } else { |
44 |
| - // // first time use when event.isUpdate = false |
45 |
| - // // service worker should claim the client immediately since its the first install. |
46 |
| - // wb.messageSW({ type: 'CLIENTS_CLAIM' }) |
47 |
| - // /* eslint-disable-next-line no-console */ |
48 |
| - // console.log('sw: clientsClaim called.') |
49 |
| - // } |
50 |
| - // }) |
| 55 | + // This code listens for the user's confirmation to update the app. |
| 56 | + wb.addEventListener('message', (event) => { |
| 57 | + console.log('sw: message event listener hit.') |
| 58 | + if (event.data && event.data.type === 'SKIP_WAITING') { |
| 59 | + wb.messageSkipWaiting() |
| 60 | + console.log('sw: message SKIP_WAITING called.') |
| 61 | + } |
| 62 | + }) |
51 | 63 |
|
52 |
| - // // This is the code piece that GenerateSW mode can't provide for us. |
53 |
| - // // This code listens for the user's confirmation to update the app. |
54 |
| - // wb.addEventListener('message', (event) => { |
55 |
| - // /* eslint-disable-next-line no-console */ |
56 |
| - // console.log('sw: message event listener hit.') |
57 |
| - // if (event.data && event.data.type === 'SKIP_WAITING') { |
58 |
| - // /* eslint-disable-next-line no-console */ |
59 |
| - // console.log('sw: message SKIP_WAITING called.') |
60 |
| - // } |
61 |
| - // }) |
62 |
| - } |
63 |
| -} |
| 64 | + wb.addEventListener('installed', (event) => { |
| 65 | + console.log('sw: installed event listener hit.') |
| 66 | + if (event.isUpdate) { |
| 67 | + // Wires up event that is listened to in the use-service-worker composable. The app-manual-update component then |
| 68 | + // prompts the user there is an update available to activate. |
| 69 | + // manualUpdateAvailable(registration) |
| 70 | + wb.messageSkipWaiting() |
| 71 | + console.log('sw: installed new version.') |
| 72 | + } else { |
| 73 | + // first time use when event.isUpdate = false |
| 74 | + // service worker should claim the client immediately since its the first install. |
| 75 | + wb.messageSW({ type: 'CLIENTS_CLAIM' }) |
| 76 | + console.log('sw: clientsClaim called.') |
| 77 | + } |
| 78 | + }) |
64 | 79 |
|
65 |
| -export default register |
| 80 | + wb.addEventListener('waiting', (event) => { |
| 81 | + console.log('sw: waiting event listener hit.') |
| 82 | + if (event.isUpdate) { |
| 83 | + // Wires up event that is listened to in the use-service-worker composable. The app-manual-update component then |
| 84 | + // prompts the user there is an update available to activate. |
| 85 | + manualUpdateAvailable(registration) |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + wb.addEventListener('controlling', (event) => { |
| 90 | + console.log('sw: controlling event listener hit.') |
| 91 | + }) |
| 92 | + |
| 93 | + wb.addEventListener('fetch', (event) => { |
| 94 | + console.log('sw: fetch event listener hit.') |
| 95 | + // wb.messageSkipWaiting() // hack to "unfreeze" stuck service worker |
| 96 | + }) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + export default register |
0 commit comments