Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): add on begin update hooks #623

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 26 additions & 5 deletions examples/vue-router/src/ReloadPrompt.vue
@@ -1,7 +1,11 @@
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { useRegisterSW } from 'virtual:pwa-register/vue'
import { pwaInfo } from 'virtual:pwa-info'

const beginUpdateServiceWorker = ref(false)
const lastBeginUpdateServiceWorkerTime = ref(0)

console.log(pwaInfo)

// replaced dyanmicaly
Expand All @@ -25,27 +29,42 @@ const {
console.log(`SW Registered: ${r}`)
}
},
onBeginUpdate() {
console.log('Begin sw update message')
beginUpdateServiceWorker.value = true
lastBeginUpdateServiceWorkerTime.value = Date.now()
},
})

watch(computed(() => [offlineReady.value, needRefresh.value]), ([offlineReadyVal, needRefreshVal]) => {
Doctor-wu marked this conversation as resolved.
Show resolved Hide resolved
if (offlineReadyVal || needRefreshVal)
beginUpdateServiceWorker.value = false
})

async function close() {
offlineReady.value = false
needRefresh.value = false
beginUpdateServiceWorker.value = false
}
</script>

<template>
<div
v-if="offlineReady || needRefresh"
class="pwa-toast"
role="alert"
>
<div v-if="offlineReady || needRefresh || beginUpdateServiceWorker" class="pwa-toast" role="alert">
<div class="message">
<span v-if="offlineReady">
App ready to work offline
</span>
<span v-else>
New content available, click on reload button to update.
</span>
<div>
<span v-if="beginUpdateServiceWorker">
Updating...
</span>
<span v-else-if="lastBeginUpdateServiceWorkerTime">
Last detect new version: {{ new Date(lastBeginUpdateServiceWorkerTime).toLocaleString() }}
</span>
</div>
</div>
<button v-if="needRefresh" @click="updateServiceWorker()">
Reload
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/preact.ts
Expand Up @@ -8,6 +8,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
onNeedRefresh,
onBeginUpdate,
onOfflineReady,
onRegistered,
onRegisteredSW,
Expand All @@ -24,6 +25,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {
setOfflineReady(true)
onOfflineReady?.()
},
onBeginUpdate,
onNeedRefresh() {
setNeedRefresh(true)
onNeedRefresh?.()
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/react.ts
Expand Up @@ -8,6 +8,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
onNeedRefresh,
onBeginUpdate,
onOfflineReady,
onRegistered,
onRegisteredSW,
Expand All @@ -24,6 +25,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {
setOfflineReady(true)
onOfflineReady?.()
},
onBeginUpdate,
onNeedRefresh() {
setNeedRefresh(true)
onNeedRefresh?.()
Expand Down
4 changes: 4 additions & 0 deletions src/client/build/register.ts
Expand Up @@ -18,6 +18,7 @@ export function registerSW(options: RegisterSWOptions = {}) {
const {
immediate = false,
onNeedRefresh,
onBeginUpdate,
onOfflineReady,
onRegistered,
onRegisteredSW,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/solid.ts
Expand Up @@ -8,6 +8,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
onNeedRefresh,
onBeginUpdate,
onOfflineReady,
onRegistered,
onRegisteredSW,
Expand All @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {

const updateServiceWorker = registerSW({
immediate,
onBeginUpdate,
onOfflineReady() {
setOfflineReady(true)
onOfflineReady?.()
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/svelte.ts
Expand Up @@ -8,6 +8,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
onNeedRefresh,
onBeginUpdate,
onOfflineReady,
onRegistered,
onRegisteredSW,
Expand All @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {

const updateServiceWorker = registerSW({
immediate,
onBeginUpdate,
onOfflineReady() {
offlineReady.set(true)
onOfflineReady?.()
Expand Down
2 changes: 2 additions & 0 deletions src/client/build/vue.ts
Expand Up @@ -8,6 +8,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {
const {
immediate = true,
onNeedRefresh,
onBeginUpdate,
onOfflineReady,
onRegistered,
onRegisteredSW,
Expand All @@ -19,6 +20,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) {

const updateServiceWorker = registerSW({
immediate,
onBeginUpdate,
onNeedRefresh() {
needRefresh.value = true
onNeedRefresh?.()
Expand Down
1 change: 1 addition & 0 deletions 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.
Expand Down