Skip to content

Instantly share code, notes, and snippets.

@jacky810124
Created March 29, 2018 07:02
Show Gist options
  • Save jacky810124/3fb9da2d0e5cef9800b56c85937cf1da to your computer and use it in GitHub Desktop.
Save jacky810124/3fb9da2d0e5cef9800b56c85937cf1da to your computer and use it in GitHub Desktop.
PWA Day06 - generic fallback(network error)
self.addEventListener('fetch', function(event) {
event.respondWith(
// Try the cache
caches
.match(event.request)
.then(function(response) {
if (response) {
return response
}
return fetch(event.request)
.then(function(response) {
if (response.status === 404) {
return caches.match('pages/404.html')
}
return response
})
})
.catch(function() {
// If both fail, show a generic fallback:
return caches.match('/offline.html')
})
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment