Skip to content

Instantly share code, notes, and snippets.

@jacky810124
Created March 29, 2018 06:58
Show Gist options
  • Save jacky810124/7c90dc4b453c4f644e9e56cb31b8e6c7 to your computer and use it in GitHub Desktop.
Save jacky810124/7c90dc4b453c4f644e9e56cb31b8e6c7 to your computer and use it in GitHub Desktop.
PWA Day06 - generic fallback
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