From 59ccbb3cff9f7a1f2f38aba0a9f30da63fd8c022 Mon Sep 17 00:00:00 2001 From: Doctorwu Date: Thu, 14 Dec 2023 18:07:48 +0800 Subject: [PATCH 1/5] feat(register): add on begin update hooks --- src/client/build/preact.ts | 2 ++ src/client/build/react.ts | 2 ++ src/client/build/register.ts | 6 ++++++ src/client/build/solid.ts | 2 ++ src/client/build/svelte.ts | 2 ++ src/client/build/vue.ts | 2 ++ src/client/type.d.ts | 1 + 7 files changed, 17 insertions(+) diff --git a/src/client/build/preact.ts b/src/client/build/preact.ts index 847817e8..2d9a19a7 100644 --- a/src/client/build/preact.ts +++ b/src/client/build/preact.ts @@ -8,6 +8,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const { immediate = true, onNeedRefresh, + onBeginUpdate, onOfflineReady, onRegistered, onRegisteredSW, @@ -24,6 +25,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { setOfflineReady(true) onOfflineReady?.() }, + onBeginUpdate, onNeedRefresh() { setNeedRefresh(true) onNeedRefresh?.() diff --git a/src/client/build/react.ts b/src/client/build/react.ts index b0105819..6f982b18 100644 --- a/src/client/build/react.ts +++ b/src/client/build/react.ts @@ -8,6 +8,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const { immediate = true, onNeedRefresh, + onBeginUpdate, onOfflineReady, onRegistered, onRegisteredSW, @@ -24,6 +25,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { setOfflineReady(true) onOfflineReady?.() }, + onBeginUpdate, onNeedRefresh() { setNeedRefresh(true) onNeedRefresh?.() diff --git a/src/client/build/register.ts b/src/client/build/register.ts index c404513c..965948dc 100644 --- a/src/client/build/register.ts +++ b/src/client/build/register.ts @@ -18,6 +18,7 @@ export function registerSW(options: RegisterSWOptions = {}) { const { immediate = false, onNeedRefresh, + onBeginUpdate, onOfflineReady, onRegistered, onRegisteredSW, @@ -81,6 +82,11 @@ export function registerSW(options: RegisterSWOptions = {}) { onNeedRefresh?.() } + // Add an event listener to detect when the new service worker + // start to be installed but not yet installed. + wb.addEventListener('installing', (event) => { + event.isUpdate && onBeginUpdate?.() + }) wb.addEventListener('installed', (event) => { if (typeof event.isUpdate === 'undefined') { if (typeof event.isExternal !== 'undefined') { diff --git a/src/client/build/solid.ts b/src/client/build/solid.ts index 76e378ea..c64bdfbb 100644 --- a/src/client/build/solid.ts +++ b/src/client/build/solid.ts @@ -8,6 +8,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const { immediate = true, onNeedRefresh, + onBeginUpdate, onOfflineReady, onRegistered, onRegisteredSW, @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const updateServiceWorker = registerSW({ immediate, + onBeginUpdate, onOfflineReady() { setOfflineReady(true) onOfflineReady?.() diff --git a/src/client/build/svelte.ts b/src/client/build/svelte.ts index f1e71cc1..132ec3cd 100644 --- a/src/client/build/svelte.ts +++ b/src/client/build/svelte.ts @@ -8,6 +8,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const { immediate = true, onNeedRefresh, + onBeginUpdate, onOfflineReady, onRegistered, onRegisteredSW, @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const updateServiceWorker = registerSW({ immediate, + onBeginUpdate, onOfflineReady() { offlineReady.set(true) onOfflineReady?.() diff --git a/src/client/build/vue.ts b/src/client/build/vue.ts index 4fd6801a..b9a0ee8a 100644 --- a/src/client/build/vue.ts +++ b/src/client/build/vue.ts @@ -8,6 +8,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const { immediate = true, onNeedRefresh, + onBeginUpdate, onOfflineReady, onRegistered, onRegisteredSW, @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { const updateServiceWorker = registerSW({ immediate, + onBeginUpdate, onNeedRefresh() { needRefresh.value = true onNeedRefresh?.() diff --git a/src/client/type.d.ts b/src/client/type.d.ts index fcb2a563..d6b1c71d 100644 --- a/src/client/type.d.ts +++ b/src/client/type.d.ts @@ -1,6 +1,7 @@ export interface RegisterSWOptions { immediate?: boolean onNeedRefresh?: () => void + onBeginUpdate?: () => void onOfflineReady?: () => void /** * Called only if `onRegisteredSW` is not provided. From 7ba5603669ab5859ccb985c33ae0dc62b1812715 Mon Sep 17 00:00:00 2001 From: Doctorwu Date: Thu, 14 Dec 2023 18:26:56 +0800 Subject: [PATCH 2/5] feat(example): add on begin update hooks example --- examples/vue-basic-cdn/src/ReloadPrompt.vue | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/vue-basic-cdn/src/ReloadPrompt.vue b/examples/vue-basic-cdn/src/ReloadPrompt.vue index 835383c5..ae5deba4 100644 --- a/examples/vue-basic-cdn/src/ReloadPrompt.vue +++ b/examples/vue-basic-cdn/src/ReloadPrompt.vue @@ -9,6 +9,9 @@ const { onRegisteredSW(swUrl) { console.log(`Service Worker at: ${swUrl}`) }, + onBeginUpdate() { + console.log('Begin to update') + }, }) async function close() { From e05594854b0def502b627a6e28e5b01a7b3980c8 Mon Sep 17 00:00:00 2001 From: Doctorwu Date: Fri, 15 Dec 2023 00:14:29 +0800 Subject: [PATCH 3/5] feat(examples & register): use update found to detect new version --- examples/vue-basic-cdn/src/ReloadPrompt.vue | 3 -- examples/vue-router/src/ReloadPrompt.vue | 31 +++++++++++++++++---- src/client/build/register.ts | 6 ++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/examples/vue-basic-cdn/src/ReloadPrompt.vue b/examples/vue-basic-cdn/src/ReloadPrompt.vue index ae5deba4..835383c5 100644 --- a/examples/vue-basic-cdn/src/ReloadPrompt.vue +++ b/examples/vue-basic-cdn/src/ReloadPrompt.vue @@ -9,9 +9,6 @@ const { onRegisteredSW(swUrl) { console.log(`Service Worker at: ${swUrl}`) }, - onBeginUpdate() { - console.log('Begin to update') - }, }) async function close() { diff --git a/examples/vue-router/src/ReloadPrompt.vue b/examples/vue-router/src/ReloadPrompt.vue index 4b60d2b6..df1769df 100644 --- a/examples/vue-router/src/ReloadPrompt.vue +++ b/examples/vue-router/src/ReloadPrompt.vue @@ -1,7 +1,11 @@