Prompting Users for Updates
You'll learn how to build UI patterns that inform users when a new service worker version is ready and let them manually trigger a refresh.
The Core Idea
When your service worker updates (using skipWaiting or not), users might still be running the old version of your app. Rather than forcing an abrupt refresh, best practice is to show a polite notification that an update is available and give users control over when to apply it. This respects their current activity (they might be filling out a form!) while keeping your app fresh.
Think of it like a mobile app that shows "Update available" in the corner—non-intrusive but actionable.
How It Works
You've already learned about the controllerchange event and update lifecycle. Now you combine those with UI:
-
Detect the update: Listen for the
updatefoundevent on your service worker registration, and watch for the new worker entering theinstalledstate while an old one is still active (meaning it's waiting). -
Show UI: Display a banner, toast, or button with a message like "New version available—Refresh to update."
-
Handle user action: When clicked, call
skipWaiting()on the waiting service worker (viapostMessage), then reload the page whencontrollerchangefires.
This pattern gives users agency—they click when ready, and the update applies smoothly without losing unsaved work or interrupting their flow.
Key Takeaway: Prompting users for updates creates a respectful, user-controlled pattern that notifies them of new versions and lets them choose when to refresh, balancing freshness with uninterrupted experience.