Service Worker Introduction

Step-by-step introduction to the Service Worker API.

Register service worker

To use a service worker you need to register it within a page.

Use Developer Tools > Resources > Service Workers to inspect the state of your Service Worker.

Code

// in page.html
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('sw.js', { scope: './' })
    .then(reg => console.info('registered sw', reg))
    .catch(err => console.error('error registering sw', err));
}

The highest scope a service worker can have is the origin its registered from.
An origin can have many service workers as long as they have different scopes.

Note: You can force an error, by registering a non-existing sw-fake.js.

Output