Clients.claim() for Immediate Control
What you'll learn: How to make a newly activated service worker take control of open pages instantly, without requiring a refresh.
The Problem with Default Behavior
By default, when a new service worker activates, it doesn't control currently open pages—only pages loaded after activation. Existing tabs remain controlled by the old service worker (or no service worker at all on first install). Users must close all tabs and reopen them to get the new service worker in charge.
Think of it like a building manager: when a new manager arrives, the old tenants keep working with the old manager until they move out and new tenants move in. clients.claim() is like announcing "the new manager is in charge of everyone immediately."
Using clients.claim()
Inside your service worker's activate event, call clients.claim():
self.addEventListener('activate', event => {
event.waitUntil(
clients.claim()
);
});
This makes the newly activated service worker take control of all pages within its scope right away—no refresh required.
Common Pattern: Claim + Skip Waiting
You'll often see clients.claim() paired with skipWaiting():
// In install event
self.addEventListener('install', event => {
self.skipWaiting(); // Don't wait for old SW to stop
});
// In activate event
self.addEventListener('activate', event => {
event.waitUntil(clients.claim()); // Control pages immediately
});
Together, they ensure your new service worker activates and controls pages as quickly as possible.
When to Use It
Use clients.claim() when you need immediate updates (critical bug fixes, security patches). Be cautious: if your new service worker behaves differently, existing pages might encounter unexpected behavior mid-session.
Key Takeaway: clients.claim() forces the newly activated service worker to immediately control all open pages, bypassing the need for a manual refresh or tab close.