aboutsummaryrefslogtreecommitdiff
path: root/modern/src/UpdateController.tsx
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2023-11-06 07:42:06 -0800
committerGitHub <noreply@github.com>2023-11-06 07:42:06 -0800
commit4a6e35cf9e3aebaee23bf8a418e84aecf6bab398 (patch)
tree560ecde1021d6ed99e51dee688ee20cbd529da1a /modern/src/UpdateController.tsx
parentd59b39c6aad3863583294dd59b616c34a1fadf35 (diff)
parent85d5322a78332175fd870e588469fc9653fa8257 (diff)
downloadtrackermap-web-4a6e35cf9e3aebaee23bf8a418e84aecf6bab398.tar.gz
trackermap-web-4a6e35cf9e3aebaee23bf8a418e84aecf6bab398.tar.bz2
trackermap-web-4a6e35cf9e3aebaee23bf8a418e84aecf6bab398.zip
Merge pull request #1188 from jinzo/pwa-sw-update-check
PWA regularly check for service worker updates and automaticially reload the page
Diffstat (limited to 'modern/src/UpdateController.tsx')
-rw-r--r--modern/src/UpdateController.tsx58
1 files changed, 58 insertions, 0 deletions
diff --git a/modern/src/UpdateController.tsx b/modern/src/UpdateController.tsx
new file mode 100644
index 00000000..0b2b7985
--- /dev/null
+++ b/modern/src/UpdateController.tsx
@@ -0,0 +1,58 @@
+import { Snackbar, IconButton } from '@mui/material';
+import RefreshIcon from '@mui/icons-material/Refresh';
+import React from 'react'
+import { useSelector } from 'react-redux';
+import { useTranslation } from './common/components/LocalizationProvider';
+import { useRegisterSW } from 'virtual:pwa-register/react'
+
+// Based on https://vite-pwa-org.netlify.app/frameworks/react.html
+function UpdateController() {
+ const t = useTranslation();
+
+ const swUpdateInterval = useSelector((state) => state.session.server.attributes.serviceWorkerUpdateInterval || 3600000);
+
+ const {
+ needRefresh: [needRefresh],
+ updateServiceWorker,
+ } = useRegisterSW({
+ onRegisteredSW(swUrl, swRegistration) {
+ if (swUpdateInterval > 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();
+ }
+ }, swUpdateInterval);
+ }
+ }
+ });
+
+ return (
+ <Snackbar
+ open={needRefresh}
+ message={t('settingsUpdateAvailable')}
+ action={(
+ <IconButton color="inherit" onClick={() => updateServiceWorker(true)}>
+ <RefreshIcon />
+ </IconButton>
+ )}
+ />
+ );
+}
+
+export default UpdateController;