From 59ccbb3cff9f7a1f2f38aba0a9f30da63fd8c022 Mon Sep 17 00:00:00 2001 From: Doctorwu Date: Thu, 14 Dec 2023 18:07:48 +0800 Subject: [PATCH] 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.