feat(web): auto-update the PWA when a new release is deployed

Register the service worker from the app (virtual:pwa-register) instead of
the injected script, so an installed PWA that stays open also checks for a
new worker every 30 minutes, when it returns to the foreground and when the
connection comes back. The new worker skips waiting, claims every tab and the
page reloads into it, deferring the reload until the app is backgrounded if
the user is typing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-19 13:15:10 -03:00
parent 56853bbdeb
commit 686325d8a4
4 changed files with 91 additions and 4 deletions

View File

@@ -0,0 +1,78 @@
import { registerSW } from 'virtual:pwa-register'
/** How often an open app asks the server for a new service worker. */
const UPDATE_INTERVAL_MS = 30 * 60 * 1000
function isEditing() {
const element = document.activeElement
if (!(element instanceof HTMLElement)) return false
return (
element.isContentEditable ||
element instanceof HTMLInputElement ||
element instanceof HTMLTextAreaElement ||
element instanceof HTMLSelectElement
)
}
/**
* Reloads into the new release as soon as it is safe: right away, unless the
* user is typing (an item name, a list name...), in which case it waits until
* the app goes to the background so nothing they wrote is lost.
*/
function reloadWhenIdle() {
if (document.visibilityState === 'hidden' || !isEditing()) {
window.location.reload()
return
}
document.addEventListener(
'visibilitychange',
() => {
if (document.visibilityState === 'hidden') window.location.reload()
},
{ once: true },
)
}
/**
* Registers the service worker and keeps the installed PWA up to date.
*
* With `registerType: 'autoUpdate'` the new worker skips waiting and claims the
* page as soon as it is installed, but the browser only looks for one on a
* navigation. An installed PWA is rarely navigated (it stays open in the
* background for days), so this also checks periodically, whenever the app
* comes back to the foreground and when the connection returns.
*/
export function registerServiceWorker() {
if (!('serviceWorker' in navigator)) return
registerSW({
immediate: true,
onNeedReload: reloadWhenIdle,
onRegisteredSW(swUrl, registration) {
if (!registration) return
const checkForUpdate = async () => {
if (registration.installing || !navigator.onLine) return
// Skip the check when the server is unreachable instead of letting
// `update()` reject (it would log an unhandled error offline).
try {
const response = await fetch(swUrl, {
cache: 'no-store',
headers: { 'cache-control': 'no-cache' },
})
if (response.ok) await registration.update()
} catch {
// Offline or server down: try again on the next trigger.
}
}
setInterval(checkForUpdate, UPDATE_INTERVAL_MS)
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') void checkForUpdate()
})
window.addEventListener('online', () => void checkForUpdate())
},
})
}

View File

@@ -4,10 +4,13 @@ import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { createQueryClient } from '@/lib/query-client'
import { registerServiceWorker } from '@/lib/register-sw'
import { routeTree } from './routeTree.gen'
import './index.css'
registerServiceWorker()
const queryClient = createQueryClient()
const router = createRouter({