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

+36
View File
@@ -3457,6 +3457,42 @@
max-width: 90vw;
}
.ts-update-toast {
position: fixed;
left: 50%;
bottom: 1.25rem;
transform: translateX(-50%);
z-index: 600;
display: flex;
align-items: center;
gap: 0.6rem;
background: var(--panel-bg, #23262e);
color: var(--text, #eee);
border: 1px solid var(--border);
border-radius: 0.5rem;
padding: 0.6rem 0.7rem 0.6rem 0.9rem;
font-size: 13px;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}
.ts-update-toast button {
border: 1px solid var(--border);
border-radius: 0.35rem;
background: var(--accent, #4a7dfc);
color: #fff;
font-size: 12px;
padding: 0.3rem 0.6rem;
cursor: pointer;
}
.ts-update-toast-dismiss {
background: transparent !important;
color: var(--text-muted, #999) !important;
border: none !important;
padding: 0 0.2rem !important;
font-size: 12px;
}
.ts-feedback-fab-wrap {
position: fixed;
right: 1.25rem;
+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)} />
)}
+6
View File
@@ -48,6 +48,9 @@ const translations: Record<Lang, Record<string, string>> = {
"menu.extras.feedback": "Feedback geben",
"feedback.fab.nudge": "Was denkst du über WebSpeak3? Feedback ist willkommen!",
"pwa.updateAvailable": "Neue Version verfügbar",
"pwa.reload": "Neu laden",
"feedback.dialog.title": "Feedback",
"feedback.dialog.intro": "Melde einen Fehler oder teile eine Idee zur Weiterentwicklung von WebSpeak3 mit.",
"feedback.dialog.category": "Kategorie",
@@ -634,6 +637,9 @@ const translations: Record<Lang, Record<string, string>> = {
"menu.extras.feedback": "Send Feedback",
"feedback.fab.nudge": "What do you think of WebSpeak3? Feedback welcome!",
"pwa.updateAvailable": "New version available",
"pwa.reload": "Reload",
"feedback.dialog.title": "Feedback",
"feedback.dialog.intro": "Report a bug or share an idea to help improve WebSpeak3.",
"feedback.dialog.category": "Category",
+3
View File
@@ -0,0 +1,3 @@
/// <reference types="vite/client" />
/// <reference types="vite-plugin-pwa/client" />
/// <reference types="vite-plugin-pwa/react" />
+8 -1
View File
@@ -7,7 +7,14 @@ export default defineConfig({
plugins: [
react(),
VitePWA({
registerType: 'autoUpdate',
registerType: 'prompt',
// We register the service worker ourselves (see useSwUpdatePrompt in
// App.tsx) via the virtual:pwa-register/react module so we can show a
// "new version available" toast instead of updating silently — a
// stale-cache tab would otherwise keep running old JS/CSS until the
// user manually reloads (or worse, clears storage), which bit us
// repeatedly while verifying deploys.
injectRegister: false,
// Dev-time Workbox would sit in front of Vite's own HMR/WS traffic and
// stale-cache the app shell while iterating — only ever ship the SW in
// production builds.