Skip to content

Instantly share code, notes, and snippets.

@jeffposnick
Created May 5, 2016 21:11
Show Gist options
  • Save jeffposnick/bd71531ee19537c568474e1f834e34fb to your computer and use it in GitHub Desktop.
Save jeffposnick/bd71531ee19537c568474e1f834e34fb to your computer and use it in GitHub Desktop.
Given a list of URLs, check to see whether each of them are already stored in a cache
const CACHE_NAME = 'my-cache'; // Or whatever your cache is.
let urls = []; // An array of full, absolute URLs that may or may not be cached.
caches.open(CACHE_NAME)
.then(cache => cache.keys())
.then(responses => responses.map(response => response.url))
.then(urlsInCache => Promise.all(urls.map(url => {
return {
url: url,
cached: urlsInCache.includes(url)
};
}))).then(urlCacheStatuses => {
// Do something with each item in the urlCacheStatuses array,
// i.e. change the display of the UI element corresponding to
// item.url if item.cached is true.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment