Last active
August 29, 2015 14:06
-
-
Save nikcorg/7760963cdee9668728db to your computer and use it in GitHub Desktop.
Immediately returning response
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
this.addEventListener('fetch', function(event) { | |
var cachedResponse = caches.match(event.request).catch(function() { | |
return event.default().then(function(response) { | |
// With the assumption that this is an async operation... | |
// (who reads specs anyway, and besides it's returning a promise) | |
// ...store the response to the cache in parallel. | |
caches.get('v1').then(function(cache) { | |
cache.put(event.request, response.clone()); | |
}); | |
// Return the response immediately, without waiting for the cache write operation to finish | |
return response; | |
}); | |
}).catch(function() { | |
return caches.match('/sw-test/gallery/myLittleVader.jpg'); | |
}); | |
event.respondWith(cachedResponse); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment