Created
March 29, 2018 07:02
-
-
Save jacky810124/3fb9da2d0e5cef9800b56c85937cf1da to your computer and use it in GitHub Desktop.
PWA Day06 - generic fallback(network error)
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) { | |
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