-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added update data loss warning
- Loading branch information
1 parent
098073b
commit b124e1d
Showing
7 changed files
with
99 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { browser } from '$app/environment'; | ||
import { useRegisterSW } from 'virtual:pwa-register/svelte'; | ||
|
||
const sw = browser | ||
? useRegisterSW({ | ||
onRegisteredSW(swUrl, r) { | ||
if (r) { | ||
setInterval(async () => { | ||
if (!navigator || r.installing) return; | ||
if ('connection' in navigator && !navigator.onLine) return; | ||
|
||
const resp = await fetch(swUrl, { | ||
cache: 'no-store', | ||
headers: { | ||
cache: 'no-store', | ||
'cache-control': 'no-cache' | ||
} | ||
}); | ||
if (resp.status === 200) await r.update(); | ||
}, 3600000); | ||
} | ||
console.log(`SW Registered: ${r}`); | ||
}, | ||
onRegisterError(error) { | ||
console.log('SW registration error', error); | ||
} | ||
}) | ||
: null; | ||
|
||
export const needRefresh = sw?.needRefresh; | ||
export const updateServiceWorker = sw?.updateServiceWorker; | ||
export const offlineReady = sw?.offlineReady; | ||
export const updateDataLossDialog = $state({ open: false }); |
27 changes: 27 additions & 0 deletions
27
src/routes/(components)/layout/UpdateDataLossDialog.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<script lang="ts"> | ||
import ResponsiveDialog from '$lib/components/ResponsiveDialog.svelte'; | ||
import Button from '$lib/components/ui/button/button.svelte'; | ||
import LoaderCircle from 'virtual:icons/lucide/loader-circle'; | ||
import { updateDataLossDialog, updateServiceWorker } from './PWAFunctions.svelte'; | ||
let updating = $state(false); | ||
function updateApp() { | ||
updating = true; | ||
localStorage.clear(); | ||
updateServiceWorker!(true); | ||
} | ||
</script> | ||
|
||
<ResponsiveDialog title="Update app?" bind:open={updateDataLossDialog.open}> | ||
{#snippet description()} | ||
<p>Any unsaved data, like a workout in progress, or an unsaved mesocycle will be lost.</p> | ||
{/snippet} | ||
<Button disabled={updating} onclick={updateApp} class="gap-2"> | ||
{#if updating} | ||
<LoaderCircle class="animate-spin" /> | ||
{:else} | ||
Update | ||
{/if} | ||
</Button> | ||
</ResponsiveDialog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters