The update-available banner
A new service worker can take over in the middle of someone's work. This pattern replaces the silent reload with a small, keyboard-friendly banner: Update available. Reload. It keeps the app usable, never clears data, and reloads only after the person says so.
Try the banner
This demo fakes the moment a new worker is ready. Nothing is installed or updated.
The banner stays hidden until an update is ready.
How it works
- Do not call
skipWaiting()on install. A new worker waits until the person chooses to reload. - Watch for a waiting worker. When
registration.waitingexists, show the banner. Check on launch and when the app comes back to the foreground. - Let “Reload” ask the worker to take over. Post
{ type: 'SKIP_WAITING' }, then reload once oncontrollerchange. - Keep “Not now” honest. Hide the banner, leave the app running, and offer it again on the next launch.
- Never clear storage. Only old caches are deleted. Run data migrations by
schemaVersion, after an automatic backup.
In the service worker
self.addEventListener('install', (event) => {
// Do not call self.skipWaiting() here: the new worker waits.
event.waitUntil(caches.open(CACHE).then((cache) => cache.addAll(FILES)));
});
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') self.skipWaiting();
}); In the app
const registration = await navigator.serviceWorker.register('/sw.js');
function offerUpdate(worker) {
// Show your banner. Reload must post to the waiting worker:
// worker.postMessage({ type: 'SKIP_WAITING' });
}
registration.addEventListener('updatefound', () => {
const worker = registration.installing;
worker?.addEventListener('statechange', () => {
if (worker.state === 'installed' && navigator.serviceWorker.controller) {
offerUpdate(registration.waiting ?? worker);
}
});
});
navigator.serviceWorker.addEventListener('controllerchange', () => {
location.reload(); // exactly once, after the person chose Reload
});
The banner is a role="region" with a heading, and its status is announced politely. Focus
moves to Reload when it appears and returns to the control that opened it when
dismissed. Escape is a second way to choose Not now.
Rules that keep it safe
- Check for an update on launch and on
visibilitychange, not in a timer loop. - Keep the versioned cache name, and delete only caches from older versions.
- Never reload while a form has unsaved changes; the banner is an offer, not a countdown.
- If the stored schema is newer than the app knows, ask the person to update instead of overwriting it.
- Serve
sw.jswithCache-Control: no-cacheso the browser sees new versions promptly.
Back to install steps or the app list.