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?
- We added an install event listener to our sw.js file, which is triggered when our service worker is registered.
- 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).
- Then we have something known as CacheStorage ...