Skip to content

Instantly share code, notes, and snippets.

@luke-denton-aligent
Last active November 16, 2016 04:01
Show Gist options
  • Save luke-denton-aligent/d86f39d2e28577b8127bcf549c12c985 to your computer and use it in GitHub Desktop.
Save luke-denton-aligent/d86f39d2e28577b8127bcf549c12c985 to your computer and use it in GitHub Desktop.
This snippet shows how to serve a cached file instead of having to load it from the network
// Offline Web Applications course on Udacity https://classroom.udacity.com/courses/ud899
//Following on from previous example here: https://gist.github.com/luke-denton/ef791e5150470814a7a155cd85b1bf80
self.addEventListener('fetch', function(event) {
event.respondWith(
//Look for a match to the current request in all caches, resolves to a Promise
caches.match(event.request).then(function(response) {
//If the repsonse is truthy, meaning something was found in the cache, then return it
if (response) return response;
//If no match was found in the cache, then make a network call to download the asset, and return that
return fetch(event.request);
})
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment