Lesson 27 of 55
javascriptService Workers – Offline Caching & PWA Basics
13 mins
Course progress49%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 27 of 55
javascript13 mins
Keep these ideas in mind as you work through the lesson.
Service workers sit between the network and pages.
self.addEventListener('install', e => {
e.waitUntil(caches.open('v1').then(cache => cache.addAll(['/','/index.html','/style.css'])));
});
self.addEventListener('fetch', e => {
e.respondWith(caches.match(e.request).then(r => r || fetch(e.request)));
});
navigator.serviceWorker.register('/sw.js').if('serviceWorker' in navigator){
navigator.serviceWorker.register('/sw.js').then(()=>console.log('SW registered'));
}