From 68e1c6dff0e289787f890cd55d87b94a6b7c5951 Mon Sep 17 00:00:00 2001 From: Andreas Richter <708186+richtera@users.noreply.github.com> Date: Wed, 30 Oct 2024 05:22:05 -0400 Subject: [PATCH 1/5] fix: Add provide('swHasUpgrade', hasUpgrade) and prevent automatic upgrade and reload of service worker --- app.vue | 27 ++++++++++++++++++++++----- service-worker/sw.ts | 13 +++++++++---- tsconfig.json | 1 + 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/app.vue b/app.vue index d79a9bff..c76fc320 100644 --- a/app.vue +++ b/app.vue @@ -216,12 +216,29 @@ useHead({ return bodyClass.join(' ') }), }, - script: [ - { - innerHTML: `if('serviceWorker' in navigator){window.addEventListener('load', () => {navigator.serviceWorker.register('/sw.js', { scope: '/' })})}`, - }, - ], }) +const hasUpgrade = ref(false) +if ('serviceWorker' in navigator) { + navigator.serviceWorker.register('/sw.js').then(registration => { + // Listen for updates + registration.addEventListener('updatefound', () => { + const newWorker = registration.installing + if (newWorker) { + newWorker.addEventListener('statechange', () => { + if (newWorker.state === 'activated') { + // Notify the user that an update is available + console.log('A new version is available. Please refresh.') + hasUpgrade.value = true + // Optionally, prompt the user for a refresh, or defer it to a better time. + } + }) + } + }) + }) +} +// Make the service worker update available to the rest of the app +// any view can use +provide('swHasUpgrade', hasUpgrade)