Skip to content

Instantly share code, notes, and snippets.

@nikcorg
Last active August 29, 2015 14:06
Show Gist options
  • Save nikcorg/7760963cdee9668728db to your computer and use it in GitHub Desktop.
Save nikcorg/7760963cdee9668728db to your computer and use it in GitHub Desktop.
Immediately returning response
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