Created
October 2, 2017 07:27
-
-
Save osvaldasvalutis/55cce16632f77be38535c17cd2f3e1ee to your computer and use it in GitHub Desktop.
Service Worker gotchas – https://medium.com/kollegorna/service-worker-gotchas-af20a9dab986
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.addEventListener('fetch', event => { | |
event.respondWith( | |
// check if the resource was cached before | |
caches.match(event.request).then(response => { | |
// serve the resource if cached, otherwise fetch it through the network | |
return response || fetch(event.request).then(response => { | |
// cache the newly fetched resource and serve it | |
let responseCopy = response.clone(); | |
addToCache(event.request, responseCopy); // this is a custom function, I'll elaborate on it later | |
return response; | |
}) | |
.catch(() => { | |
// serve "offline" page if it couldn't be fetched from network | |
return caches.match('/offline/'); | |
}); | |
}); | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment