From 0daf321f1492d6ba9e30ff5bf2ff91e44809374f Mon Sep 17 00:00:00 2001 From: Matjaž Črnko Date: Sun, 5 Nov 2023 19:54:57 +0100 Subject: fix: PWA regularly check for service worker updates and automaticially reload the app. --- modern/src/index.jsx | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'modern/src') 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(); -- cgit v1.2.3