Skip to content

Commit cf50a64

Browse files
ralyodioclaude
andauthored
fix: service worker revalidates pages too; version + Reload app in Settings (0.12.3) (#326)
0.12.2 sent pages as no-cache, but the installed iOS app still showed the old build: its service worker fetched pages with a plain fetch(), which let the browser hand back the copy it already held (cached under the old stale-while-revalidate header for up to a year). - public/sw.js (cache v3): page loads use fetch(request, { cache: 'no-cache' }) so the browser always checks with the server (a 304 when unchanged), and the offline shell is precached with cache: 'reload'. - When a new worker takes over, the app reloads into it once (controllerchange), or offers the Update toast if a draft, voice mode or a call is in progress. - Settings shows "Version <build id>" with a Reload app button: the installed app has no reload button, and this says which build is running. Tested in Chromium on a standalone production build: old v2 worker replaced by v3, the open page reloaded exactly once, pages then load through the worker (200), and offline still falls back to the cached shell. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6dcb5d4 commit cf50a64

4 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "qryptchat-web",
33
"private": true,
4-
"version": "0.12.2",
4+
"version": "0.12.3",
55
"type": "module",
66
"bin": {
77
"qc": "./bin/qc.js",

‎public/sw.js‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
*/
55

66
// Bumped to v1 -> v2 so clients running the old cache-first worker drop the
7-
// stale app shell they precached on install.
8-
const CACHE_NAME = 'qryptchat-v2';
7+
// stale app shell they precached on install. v3: page loads skip the HTTP
8+
// cache's stale copies (see the fetch handler).
9+
const CACHE_NAME = 'qryptchat-v3';
910
const OFFLINE_SHELL = '/';
1011
const STATIC_CACHE_URLS = [
1112
OFFLINE_SHELL,
@@ -40,7 +41,8 @@ self.addEventListener('install', (event) => {
4041
caches.open(CACHE_NAME)
4142
.then((cache) => {
4243
console.log('Opened cache');
43-
return cache.addAll(STATIC_CACHE_URLS);
44+
// 'reload' skips the HTTP cache, so the offline shell is never a stale copy.
45+
return cache.addAll(STATIC_CACHE_URLS.map((u) => new Request(u, { cache: 'reload' })));
4446
})
4547
.then(() => {
4648
// Skip waiting to activate immediately
@@ -87,9 +89,13 @@ self.addEventListener('fetch', (event) => {
8789
// Page loads are network-first with the cached shell as an offline fallback.
8890
// Cache-first here pinned the installed app to the HTML captured at install
8991
// time, whose hashed Next.js chunks stop existing after the next deploy.
92+
//
93+
// cache: 'no-cache' makes the browser check with the server every time. Pages
94+
// used to be served with stale-while-revalidate for a year, and a plain
95+
// fetch() here let the browser hand back that stale copy instead.
9096
if (request.mode === 'navigate') {
9197
event.respondWith(
92-
fetch(request)
98+
fetch(request, { cache: 'no-cache' })
9399
.then((response) => {
94100
if (response.ok && url.pathname === OFFLINE_SHELL) {
95101
const copy = response.clone();

‎src/app/settings/page.jsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import InviteAnon from '@/lib/components/settings/InviteAnon.jsx';
1111
import PasskeySettings from '@/lib/components/settings/PasskeySettings.jsx';
1212
import ProfileSettings from '@/lib/components/settings/ProfileSettings.jsx';
1313
import AgentSettings from '@/lib/components/settings/AgentSettings.jsx';
14+
import { CURRENT_BUILD } from '@/lib/app-update.js';
1415

1516
export default function SettingsPage() {
1617
const router = useRouter();
@@ -26,6 +27,11 @@ export default function SettingsPage() {
2627
<div className="settings-page">
2728
<div className="container">
2829
<h1>Settings</h1>
30+
{/* The installed iOS app has no reload button: this says which build is running and reloads it. */}
31+
<p className="settings-version">
32+
Version {CURRENT_BUILD || 'unknown'}
33+
<button type="button" onClick={() => window.location.reload()}>Reload app</button>
34+
</p>
2935
<div className="settings-grid">
3036
<section className="settings-section-card">
3137
<h2>Profile</h2>
@@ -63,7 +69,9 @@ export default function SettingsPage() {
6369
</div>
6470
<style>{`
6571
.settings-page { padding: 2rem 0; }
66-
.settings-page h1 { font-size: 2rem; font-weight: 700; margin-bottom: 2rem; color: var(--color-text-primary); }
72+
.settings-page h1 { font-size: 2rem; font-weight: 700; margin-bottom: .5rem; color: var(--color-text-primary); }
73+
.settings-version { display: flex; align-items: center; gap: .75rem; margin-bottom: 2rem; font-size: .8125rem; color: var(--color-text-muted); font-family: var(--font-mono, ui-monospace, monospace); }
74+
.settings-version button { font: inherit; font-family: inherit; padding: .2rem .6rem; border: 1px solid var(--color-border-primary); border-radius: 1rem; background: var(--color-bg-secondary); color: var(--color-text-primary); cursor: pointer; }
6775
.settings-grid { display: grid; gap: 1.5rem; grid-template-columns: repeat(auto-fill, minmax(400px, 1fr)); }
6876
.settings-section-card { background: var(--color-bg-primary); border: 1px solid var(--color-border-primary); border-radius: 1rem; padding: 1.5rem; }
6977
.settings-section-card h2 { font-size: 1.125rem; font-weight: 600; margin-bottom: 1rem; color: var(--color-text-primary); }

‎src/lib/components/PWAToastManager.jsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ export default function PWAToastManager() {
1919

2020
useEffect(() => {
2121
if ('serviceWorker' in navigator) {
22+
// A new worker took over (it activates at once): reload into it unless
23+
// something is in progress. Not on first install, when there was no worker.
24+
const hadWorker = !!navigator.serviceWorker.controller;
25+
const onControllerChange = () => {
26+
if (!hadWorker) return;
27+
if (isBusy() || useChatStore.getState().activeCall) setUpdateAvailable(true);
28+
else window.location.reload();
29+
};
30+
navigator.serviceWorker.addEventListener('controllerchange', onControllerChange);
2231
navigator.serviceWorker.ready.then((reg) => {
2332
registration.current = reg;
2433
reg.addEventListener('updatefound', () => {
@@ -32,6 +41,7 @@ export default function PWAToastManager() {
3241
}
3342
});
3443
});
44+
return () => navigator.serviceWorker.removeEventListener('controllerchange', onControllerChange);
3545
}
3646
}, []);
3747

0 commit comments

Comments
 (0)