diff options
author | Matjaž Črnko <m.crnko@txt.si> | 2023-11-06 00:15:40 +0100 |
---|---|---|
committer | Matjaž Črnko <m.crnko@txt.si> | 2023-11-06 00:15:40 +0100 |
commit | d1c0292bc88f6c98e5598d1dd8b66c2ad1ea07bd (patch) | |
tree | 947c347000c7d73b1a67cd489ecaadb5615f035f /modern/src/ReloadPrompt.tsx | |
parent | 0daf321f1492d6ba9e30ff5bf2ff91e44809374f (diff) | |
download | trackermap-web-d1c0292bc88f6c98e5598d1dd8b66c2ad1ea07bd.tar.gz trackermap-web-d1c0292bc88f6c98e5598d1dd8b66c2ad1ea07bd.tar.bz2 trackermap-web-d1c0292bc88f6c98e5598d1dd8b66c2ad1ea07bd.zip |
PWA: Move to a Reload Prompt model - WIP
Diffstat (limited to 'modern/src/ReloadPrompt.tsx')
-rw-r--r-- | modern/src/ReloadPrompt.tsx | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/modern/src/ReloadPrompt.tsx b/modern/src/ReloadPrompt.tsx new file mode 100644 index 00000000..1f93f913 --- /dev/null +++ b/modern/src/ReloadPrompt.tsx @@ -0,0 +1,61 @@ +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 ReloadPrompt() { + const t = useTranslation(); + + // const serviceWorkerUpdateInterval = useAttributePreference('serviceWorkerUpdateInterval', 3600000); + const serviceWorkerUpdateInterval = 60000; + + const { + offlineReady: [offlineReady, setOfflineReady], + needRefresh: [needRefresh, setNeedRefresh], + updateServiceWorker, + } = useRegisterSW({ + onRegisteredSW(swUrl, r) { + console.log('serviceWorkerUpdateCheck', serviceWorkerUpdateInterval); + + serviceWorkerUpdateInterval > 0 && r && setInterval(async () => { + if (!(!r.installing && navigator)) { + 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(); + } + }, serviceWorkerUpdateInterval) + } + }); + + return ( + <Snackbar + open={needRefresh} + message={t('updateAvailable')} + action={( + <Button color="secondary" size="small" onClick={() => updateServiceWorker(true)}> + {t('sharedReload')} + </Button> + )} + /> + ); +} + +export default ReloadPrompt;
\ No newline at end of file |