feat: add pwa update prompt
This commit is contained in:
1 parent
5582e49be9
commit
b94e294dfb
3 files changed
+46
-17
No files matched your search
@@ -0,0 +1,36 @@
|
||||
<script lang="ts">
|
||||
import { toast as showToast } from '$lib/components/ui/toasts/toasts.js'
|
||||
import { useRegisterSW } from 'virtual:pwa-register/svelte'
|
||||
const { needRefresh, updateServiceWorker, offlineReady } = useRegisterSW({
|
||||
onRegistered(r) {
|
||||
// uncomment following code if you want check for updates
|
||||
// r && setInterval(() => {
|
||||
// console.log('Checking for sw update')
|
||||
// r.update()
|
||||
// }, 20000 /* 20s for testing purposes */)
|
||||
console.log(`SW Registered: ${r}`)
|
||||
},
|
||||
onRegisterError(error) {
|
||||
console.log('SW registration error', error)
|
||||
},
|
||||
})
|
||||
|
||||
const close = () => {
|
||||
offlineReady.set(false)
|
||||
needRefresh.set(false)
|
||||
}
|
||||
|
||||
$: toast = $offlineReady || $needRefresh
|
||||
|
||||
const t = () => {
|
||||
if (!$offlineReady && $needRefresh) {
|
||||
showToast({
|
||||
content: 'Updates available. Click the check to update.',
|
||||
action: () => updateServiceWorker(true),
|
||||
duration: 9999 * 1000,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
$: if (toast) t()
|
||||
</script>
|
||||
Reference in new issue
Block a user