Created
December 27, 2015 14:10
-
-
Save adrianholovaty/3dc22c384097d705a239 to your computer and use it in GitHub Desktop.
Example usage of serviceworker-cache-wrapper.js
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
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; | |
}); | |
}) | |
); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://www.holovaty.com/writing/service-worker-cache-names/ for explanation.