diff options
author | Matjaž Črnko <m.crnko@txt.si> | 2023-11-06 10:05:14 +0100 |
---|---|---|
committer | Matjaž Črnko <m.crnko@txt.si> | 2023-11-06 10:05:14 +0100 |
commit | 257517f115d116e57bf63a4accf4e76361da0f3d (patch) | |
tree | 4e2b0ecce9da49180a5171467449661cf25bd776 /modern/src/UpdateCheckPrompt.tsx | |
parent | a0b14adfe269fbf4cf3f31811271b97512ee99c8 (diff) | |
download | trackermap-web-257517f115d116e57bf63a4accf4e76361da0f3d.tar.gz trackermap-web-257517f115d116e57bf63a4accf4e76361da0f3d.tar.bz2 trackermap-web-257517f115d116e57bf63a4accf4e76361da0f3d.zip |
PWA: Update check rework per feedback
Diffstat (limited to 'modern/src/UpdateCheckPrompt.tsx')
-rw-r--r-- | modern/src/UpdateCheckPrompt.tsx | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/modern/src/UpdateCheckPrompt.tsx b/modern/src/UpdateCheckPrompt.tsx new file mode 100644 index 00000000..096cca8a --- /dev/null +++ b/modern/src/UpdateCheckPrompt.tsx @@ -0,0 +1,57 @@ +import { Snackbar, Button } from '@mui/material'; +import React from 'react' +import { useTranslation } from './common/components/LocalizationProvider'; +import { useAttributePreference } from './common/util/preferences'; +import { useRegisterSW } from 'virtual:pwa-register/react' + +// Based on https://vite-pwa-org.netlify.app/frameworks/react.html +function UpdateCheckPrompt() { + const t = useTranslation(); + + const serviceWorkerUpdateInterval = useAttributePreference('serviceWorkerUpdateInterval', 3600000); + + const { + needRefresh: [needRefresh], + updateServiceWorker, + } = useRegisterSW({ + onRegisteredSW(swUrl, swRegistration) { + if (serviceWorkerUpdateInterval > 0 && swRegistration) { + setInterval(async () => { + if (!(!swRegistration.installing && navigator)) { + return; + } + + if (('connection' in navigator) && !navigator.onLine) { + return; + } + + const newSW = await fetch(swUrl, { + cache: 'no-store', + headers: { + 'cache': 'no-store', + 'cache-control': 'no-cache', + }, + }); + + if (newSW?.status === 200) { + await swRegistration.update(); + } + }, serviceWorkerUpdateInterval); + } + } + }); + + return ( + <Snackbar + open={needRefresh} + message={t('settingsUpdateAvailable')} + action={( + <Button onClick={() => updateServiceWorker(true)}> + {t('settingsReload')} + </Button> + )} + /> + ); +} + +export default UpdateCheckPrompt; |