A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.
fix: resolves incorrect serving of cached frontend on new release
towfiqi61fc5f81bdac
diff --git a/frontend/src/hooks/usePwaAutoUpdate.ts b/frontend/src/hooks/usePwaAutoUpdate.tsindex a9c9c31..7a4b7ed 100644--- a/frontend/src/hooks/usePwaAutoUpdate.ts+++ b/frontend/src/hooks/usePwaAutoUpdate.ts@@ -1,9 +1,20 @@-import { useEffect } from 'react';+import { useEffect, useRef } from 'react';import { useRegisterSW } from 'virtual:pwa-register/react';+const VERSION_STORAGE_KEY = 'pluton_app_version';+/**- * This hook handles the PWA auto-update logic in the background.- * It's completely silent and requires no user interaction.+ * Handles two PWA update scenarios:+ *+ * 1. **Workbox precache update** — When a new frontend build is deployed, Workbox detects+ * that the precache manifest changed and sets `needRefresh`. We immediately activate+ * the new service worker so the page reloads with fresh assets.+ *+ * 2. **Backend version mismatch** — Compares the server's `X-App-Version` header+ * (stored on `window.plutonVersion` by `validateAuth`) against the last known version+ * in localStorage. On mismatch (e.g. docker/binary upgrade), unregisters all service+ * workers, clears all caches, and hard-reloads. This catches cases where the SW update+ * check hasn't fired yet (long-lived tabs, cached sw.js, etc.).*/export function usePwaAutoUpdate() {const {@@ -12,22 +23,51 @@ export function usePwaAutoUpdate() {updateServiceWorker,} = useRegisterSW();+ const versionCheckedRef = useRef(false);++ // Workbox detected a new service worker with updated assetsuseEffect(() => {- // This effect triggers when a new service worker is ready to take over.if (needRefresh) {- // This immediately tells the new service worker to take control,- // which will force a hard reload of the page.updateServiceWorker(true);}-- // This effect triggers when the app's assets are fully cached and ready for offline use.- // We won't show a toast, but we'll log it for debugging purposes.if (offlineReady) {console.log('PWA is ready for offline use.');- // Reset the state to prevent this log from firing on every render.setOfflineReady(false);}}, [needRefresh, offlineReady, setOfflineReady, updateServiceWorker]);- // This hook doesn't need to return anything as it performs its job in the background.+ // Backend version mismatch check+ useEffect(() => {+ if (versionCheckedRef.current) return;++ const serverVersion = (window as any).plutonVersion;+ if (!serverVersion || serverVersion === 'unknown') return;++ const storedVersion = localStorage.getItem(VERSION_STORAGE_KEY);++ if (storedVersion && storedVersion !== serverVersion) {+ versionCheckedRef.current = true;+ clearCachesAndReload(serverVersion);+ } else if (!storedVersion) {+ localStorage.setItem(VERSION_STORAGE_KEY, serverVersion);+ }+ });+}++async function clearCachesAndReload(newVersion: string) {+ try {+ if ('serviceWorker' in navigator) {+ const registrations = await navigator.serviceWorker.getRegistrations();+ await Promise.all(registrations.map((r) => r.unregister()));+ }+ if ('caches' in window) {+ const cacheNames = await caches.keys();+ await Promise.all(cacheNames.map((name) => caches.delete(name)));+ }+ } catch (e) {+ console.error('Failed to clear caches:', e);+ }++ localStorage.setItem(VERSION_STORAGE_KEY, newVersion);+ window.location.reload();}