From f81bf48cb9cd3541bf8f111770fdd42c4d8e0955 Mon Sep 17 00:00:00 2001 From: Matjaž Črnko Date: Mon, 6 Nov 2023 15:42:28 +0100 Subject: PWA: Rename to UpdateController --- modern/src/App.jsx | 4 +-- modern/src/UpdateCheckPrompt.tsx | 58 ---------------------------------------- modern/src/UpdateController.tsx | 58 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 60 deletions(-) delete mode 100644 modern/src/UpdateCheckPrompt.tsx create mode 100644 modern/src/UpdateController.tsx diff --git a/modern/src/App.jsx b/modern/src/App.jsx index 631b9718..4fe34f64 100644 --- a/modern/src/App.jsx +++ b/modern/src/App.jsx @@ -8,7 +8,7 @@ import SocketController from './SocketController'; import CachingController from './CachingController'; import { useEffectAsync } from './reactHelper'; import { sessionActions } from './store'; -import UpdateCheckPrompt from './UpdateCheckPrompt'; +import UpdateController from './UpdateController'; const useStyles = makeStyles(() => ({ page: { @@ -49,7 +49,7 @@ const App = () => { <> - +
diff --git a/modern/src/UpdateCheckPrompt.tsx b/modern/src/UpdateCheckPrompt.tsx deleted file mode 100644 index a3906304..00000000 --- a/modern/src/UpdateCheckPrompt.tsx +++ /dev/null @@ -1,58 +0,0 @@ -import { Snackbar, IconButton } from '@mui/material'; -import RefreshIcon from '@mui/icons-material/Refresh'; -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 ( - updateServiceWorker(true)}> - - - )} - /> - ); -} - -export default UpdateCheckPrompt; diff --git a/modern/src/UpdateController.tsx b/modern/src/UpdateController.tsx new file mode 100644 index 00000000..0f1b800b --- /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 { 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 UpdateController() { + 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 ( + updateServiceWorker(true)}> + + + )} + /> + ); +} + +export default UpdateController; -- cgit v1.2.3