diff --git a/examples/vue-router/src/ReloadPrompt.vue b/examples/vue-router/src/ReloadPrompt.vue index 4b60d2b6..c6d11f14 100644 --- a/examples/vue-router/src/ReloadPrompt.vue +++ b/examples/vue-router/src/ReloadPrompt.vue @@ -1,7 +1,11 @@ - + App ready to work offline @@ -46,6 +57,14 @@ async function close() { New content available, click on reload button to update. + + + Updating... + + + Last detect new version: {{ new Date(lastBeginUpdateServiceWorkerTime).toLocaleString() }} + + Reload @@ -69,9 +88,11 @@ async function close() { text-align: left; box-shadow: 3px 4px 5px 0px #8885; } + .pwa-toast .message { margin-bottom: 8px; } + .pwa-toast button { border: 1px solid #8885; outline: none; 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..8232c302 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, @@ -110,6 +111,9 @@ export function registerSW(options: RegisterSWOptions = {}) { // register the service worker wb.register({ immediate }).then((r) => { + r?.addEventListener("updatefound", () => { + onBeginUpdate?.() + }) if (onRegisteredSW) onRegisteredSW('__SW__', r) else 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.