Installing service workers

Once your worker is registered, an install event is triggered inside your service worker file. Here, we'll set up caching of our resources. A lot of new terms are coming your way; hold tight:

// sw.jsself.addEventListener('install', e => {    e.waitUntil(async function() {        const cache = await caches.open('cacheArea');        await cache.addAll(['/', '/styles/main.css', '/styles/about.css']);    }());});

Okay! What happened?

  1. We added an install event listener to our sw.js file, which is triggered when our service worker is registered.
  2. e.waitUntil accepts a promise (and we do give it a promise; remember that the async function returns a promise, and we invoked that function, as well).
  3. Then we have something known as CacheStorage ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.