Created
March 29, 2018 06:58
-
-
Save jacky810124/7c90dc4b453c4f644e9e56cb31b8e6c7 to your computer and use it in GitHub Desktop.
PWA Day06 - generic fallback
This file contains 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', function(event) { | |
event.respondWith( | |
// Try the cache | |
caches | |
.match(event.request) | |
.then(function(response) { | |
// Fall back to network | |
return response || fetch(event.request) | |
}) | |
.catch(function() { | |
// If both fail, show a generic fallback: | |
return caches.match('/offline.html') | |
// However, in reality you'd have many different | |
// fallbacks, depending on URL & headers. | |
// Eg, a fallback silhouette image for avatars. | |
}) | |
) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment