Add PWA update prompt instead of silent stale caching

Previously the service worker used registerType: 'autoUpdate' with
the default injected register script - a new deploy would install
and activate a new SW in the background, but an already-open tab
kept running the old JS/CSS from memory with no indication a newer
version existed, short of a manual hard reload (or clearing SW/
caches by hand, which we had to do repeatedly while verifying
deploys this session).

Switch to registerType: 'prompt' and register the SW ourselves via
virtual:pwa-register/react (useRegisterSW), showing a small "new
version available" toast with a reload button when an update is
waiting. Also poll registration.update() hourly so long-lived tabs
notice updates without needing a fresh navigation.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
MoepchiandClaude Sonnet 5 committed 2026-09-10 09:13:15 +02:00
1 parent dc35526727
commit 5b5f4caf79
5 files changed
+88 -1

No files matched your search

+35
View File
@@ -1,4 +1,5 @@
import { useEffect, useMemo, useRef, useState } from "react";
import { useRegisterSW } from "virtual:pwa-register/react";
import "./App.css";
/** Only dismiss on a genuine backdrop click, not a text-selection drag that
@@ -1566,6 +1567,38 @@ function ChannelPasswordDialog({
);
}
function UpdatePrompt() {
const t = useT();
const {
needRefresh: [needRefresh, setNeedRefresh],
updateServiceWorker,
} = useRegisterSW({
onRegisteredSW(_url, registration) {
// Poll for a new service worker in the background — otherwise an app
// left open for hours/days only checks for updates on next full load.
if (!registration) return;
const CHECK_INTERVAL_MS = 60 * 60 * 1000;
window.setInterval(() => {
registration.update().catch(() => {});
}, CHECK_INTERVAL_MS);
},
});
if (!needRefresh) return null;
return (
<div className="ts-update-toast">
<span>{t("pwa.updateAvailable")}</span>
<button type="button" onClick={() => updateServiceWorker(true)}>
{t("pwa.reload")}
</button>
<button type="button" className="ts-update-toast-dismiss" onClick={() => setNeedRefresh(false)}>
✕
</button>
</div>
);
}
const FEEDBACK_BUBBLE_STORAGE_KEY = "webspeak3.feedbackBubble.lastShown";
const FEEDBACK_BUBBLE_MIN_INTERVAL_MS = 3 * 24 * 60 * 60 * 1000; // don't nag more than every 3 days
const FEEDBACK_BUBBLE_INITIAL_DELAY_MS = 45_000;
@@ -8642,6 +8675,8 @@ function AppInner() {
/>
)}
<UpdatePrompt />
{IS_OWN_HOSTED_INSTANCE && !feedbackOpen && (
<FeedbackFab onOpen={() => setFeedbackOpen(true)} />
)}