Skip to content

Instantly share code, notes, and snippets.

@adrianholovaty
Created December 27, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianholovaty/3dc22c384097d705a239 to your computer and use it in GitHub Desktop.
Save adrianholovaty/3dc22c384097d705a239 to your computer and use it in GitHub Desktop.
Example usage of serviceworker-cache-wrapper.js
var jsonDataRe = /example\.com\/(.*\.json)/;
self.addEventListener('fetch', function(event) {
var request = event.request,
match = jsonDataRe.exec(request.url);
if (match) {
// Use regex capturing to grab only the bit of the URL
// that we care about, ignoring query string, etc.
var cacheKey = match[1];
event.respondWith(
cacheGet(cacheKey).then(function(response) {
return response || fetch(request).then(function(response) {
cachePut(cacheKey, request, response.clone());
return response;
});
})
);
}
});
@adrianholovaty
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment