diff --git a/docs/frameworks/index.md b/docs/frameworks/index.md index fcb900d6..fbe0607a 100644 --- a/docs/frameworks/index.md +++ b/docs/frameworks/index.md @@ -30,6 +30,7 @@ You can find all the `vite-plugin-pwa` virtual modules declarations in the follo declare module 'virtual:pwa-register' { export interface RegisterSWOptions { immediate?: boolean + onBeginUpdate?: () => void onNeedRefresh?: () => void onOfflineReady?: () => void /** diff --git a/docs/frameworks/preact.md b/docs/frameworks/preact.md index cca556b6..d67e835f 100644 --- a/docs/frameworks/preact.md +++ b/docs/frameworks/preact.md @@ -23,6 +23,7 @@ declare module 'virtual:pwa-register/preact' { export interface RegisterSWOptions { immediate?: boolean + onBeginUpdate?: () => void onNeedRefresh?: () => void onOfflineReady?: () => void /** diff --git a/docs/frameworks/react.md b/docs/frameworks/react.md index 11fe8efb..52542889 100644 --- a/docs/frameworks/react.md +++ b/docs/frameworks/react.md @@ -23,6 +23,7 @@ declare module 'virtual:pwa-register/react' { export interface RegisterSWOptions { immediate?: boolean + onBeginUpdate?: () => void onNeedRefresh?: () => void onOfflineReady?: () => void /** diff --git a/docs/frameworks/solidjs.md b/docs/frameworks/solidjs.md index 93256003..784ad8d7 100644 --- a/docs/frameworks/solidjs.md +++ b/docs/frameworks/solidjs.md @@ -23,6 +23,7 @@ declare module 'virtual:pwa-register/solid' { export interface RegisterSWOptions { immediate?: boolean + onBeginUpdate?: () => void onNeedRefresh?: () => void onOfflineReady?: () => void /** diff --git a/docs/frameworks/svelte.md b/docs/frameworks/svelte.md index dcd7a676..1c546ce6 100644 --- a/docs/frameworks/svelte.md +++ b/docs/frameworks/svelte.md @@ -23,6 +23,7 @@ declare module 'virtual:pwa-register/svelte' { export interface RegisterSWOptions { immediate?: boolean + onBeginUpdate?: () => void onNeedRefresh?: () => void onOfflineReady?: () => void /** diff --git a/docs/frameworks/vue.md b/docs/frameworks/vue.md index f61797f2..4eb7df40 100644 --- a/docs/frameworks/vue.md +++ b/docs/frameworks/vue.md @@ -21,6 +21,7 @@ declare module 'virtual:pwa-register/vue' { export interface RegisterSWOptions { immediate?: boolean + onBeginUpdate?: () => void onNeedRefresh?: () => void onOfflineReady?: () => void /** 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.