diff options
author | Matjaž Črnko <m.crnko@txt.si> | 2023-11-05 19:54:57 +0100 |
---|---|---|
committer | Matjaž Črnko <m.crnko@txt.si> | 2023-11-05 19:54:57 +0100 |
commit | 0daf321f1492d6ba9e30ff5bf2ff91e44809374f (patch) | |
tree | 5671e15fc6797492d96949db5f885451f406070b /modern/src | |
parent | d59b39c6aad3863583294dd59b616c34a1fadf35 (diff) | |
download | trackermap-web-0daf321f1492d6ba9e30ff5bf2ff91e44809374f.tar.gz trackermap-web-0daf321f1492d6ba9e30ff5bf2ff91e44809374f.tar.bz2 trackermap-web-0daf321f1492d6ba9e30ff5bf2ff91e44809374f.zip |
fix: PWA regularly check for service worker updates and automaticially reload the app.
Diffstat (limited to 'modern/src')
-rw-r--r-- | modern/src/index.jsx | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/modern/src/index.jsx b/modern/src/index.jsx index 3eb96d2f..33524e97 100644 --- a/modern/src/index.jsx +++ b/modern/src/index.jsx @@ -16,6 +16,33 @@ import NativeInterface from './common/components/NativeInterface'; import ServerProvider from './ServerProvider'; import ErrorBoundary from './ErrorBoundary'; import AppThemeProvider from './AppThemeProvider'; +import { registerSW } from 'virtual:pwa-register' + +const ServiceWorkerUpdateCheckInterval = 60 * 60 * 1000; + +registerSW({ + onRegisteredSW(swUrl, r) { + 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() + }, ServiceWorkerUpdateCheckInterval) + }, + immediate: true +}); preloadImages(); |