Created
December 27, 2015 14:06
-
-
Save adrianholovaty/0568c895fafd9417e13c to your computer and use it in GitHub Desktop.
Service worker cache wrapper that lets you use arbitrary cache keys
This file contains hidden or 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
function cachePut(key, request, response) { | |
caches.open(key).then(function(cache) { | |
cache.put(request, response); | |
}); | |
} | |
function cacheGet(key) { | |
return new Promise(function(resolve, reject) { | |
caches.open(key).then(function(cache) { | |
cache.keys().then(function(keyArray) { | |
if (keyArray.length) { | |
cache.match(keyArray[0]).then(function(response) { | |
resolve(response); | |
}); | |
} | |
else { | |
resolve(null); | |
} | |
}); | |
}); | |
}); | |
} |
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.