feat: auto-update PWA when new version available
All checks were successful
Build & Deploy / build-and-deploy (push) Successful in 35s

Uses registerSW to detect new service worker versions and reload
automatically. Defers reload if file transfers are active, checking
every 30s until transfers complete.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ordinarthur 2026-04-14 13:27:40 +02:00
parent dd37b49ce4
commit acda1a8bb8
2 changed files with 27 additions and 1 deletions

View File

@ -1,9 +1,35 @@
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
import { registerSW } from "virtual:pwa-register";
import App from "./App";
import { useStore } from "./stores/useStore";
import "./index.css";
// Auto-reload when a new service worker is available
const updateSW = registerSW({
onNeedRefresh() {
const hasActiveTransfers = useStore
.getState()
.transfers.some((t) => t.status === "transferring" || t.status === "pending");
if (hasActiveTransfers) {
// Defer update — check again every 30s
const interval = setInterval(() => {
const still = useStore
.getState()
.transfers.some((t) => t.status === "transferring" || t.status === "pending");
if (!still) {
clearInterval(interval);
updateSW(true);
}
}, 30_000);
} else {
updateSW(true);
}
},
onOfflineReady() {},
});
createRoot(document.getElementById("root")!).render(
<StrictMode>
<BrowserRouter>

View File

@ -3,7 +3,7 @@
"compilerOptions": {
"outDir": "./dist",
"jsx": "react-jsx",
"types": ["vite/client"],
"types": ["vite/client", "vite-plugin-pwa/client"],
"declaration": false,
"declarationMap": false
},