ServiceWorkerGlobalScope: activate event

The activate event of the ServiceWorkerGlobalScope interface is fired when a ServiceWorkerRegistration acquires a new ServiceWorkerRegistration.active worker.

Bubbles No
Cancelable No
Interface ExtendableEvent
Event handler property ServiceWorkerGlobalScope.onactivate

Examples

The following snippet shows how you could use an activate event handler to upgrade a cache.

globalScope.addEventListener('activate', function(event) {
  var cacheWhitelist = ['v2'];

  event.waitUntil(
    caches.forEach(function(cache, cacheName) {
      if (cacheWhitelist.indexOf(cacheName) == -1) {
        return caches.delete(cacheName);
      }
    })
  );
});

You can also set up the event handler using the ServiceWorkerGlobalScope.onactivate property:

globalScope.onactivate = function(event) {
  ...
};

Specifications

Specification
Service Workers 1
# service-worker-global-scope-activate-event

Browser compatibility

BCD tables only load in the browser

See also