Course contentsShow
← Web Development
Lesson 2374 of 2,98243. Service Workers Deep DiveFree lesson

The Activated State

When a service worker becomes active and gains control of pages, including the activate event.

The Activated State

You'll learn when and how a service worker becomes active and takes control of your web pages.

When Activation Happens

After a service worker finishes installing (and potentially waiting), it enters the activated state. This is the moment when the service worker officially takes over, gaining control of all pages within its scope. The browser fires an activate event during this transition.

Think of it like a shift change at a restaurant: the old manager (previous service worker) clocks out, and the new manager (newly activated service worker) takes charge of all operations going forward.

The Activate Event

The activate event is your opportunity to perform cleanup tasks, particularly removing old caches from previous service worker versions:

self.addEventListener('activate', event => {
  console.log('Service worker activated!');
  
  event.waitUntil(
    caches.keys().then(cacheNames => {
      return Promise.all(
        cacheNames
          .filter(name => name !== 'my-cache-v2')
          .map(name => caches.delete(name))
      );
    })
  );
});

Taking Control

Once activated, the service worker controls navigation and resource requests for pages within its scope. However, by default it won't control pages that were already open when it activated—only new page loads. (You learned about clients.claim() to override this behavior in "Claiming Clients Immediately".)

Why It Matters

The activated state represents full operational control. Your fetch event listeners are now active, your caching strategies are live, and you're ready to handle offline scenarios or background sync.

Key Takeaway: The activated state means your service worker is now in control and handling requests—use the activate event to clean up old resources before taking charge.