I am using workbox in my project but the problem is not specific to workbox.
Can anyone tell me how to retrieve list of all urls from specific cache and return it to callback function. I am trying to show users list of all cached urls and an option to delete them. But i am not able to populate/show list of all cached urls.
What i am doing is:
function queryCache(){
var url = [];
caches.open('mycache').then(function (cache){
cache.keys().then(function(keys){
return new Promise(function(resolve, reject) {
keys.forEach(function (request, index, array) {
cache.match(request).then(function (response){
url.push(response.url);
})
})
resolve();
});
}).then(function(){
console.log(url); // lists all urls in console as array
cacheList(url);
});
})
}
function cacheList(Items)
{ for(var i = 0; i < Items.length; i++){
console.log('Items: '+Items[i]); //never reaches
}
console.log(Items.length); // always show 0
}
when i call queryCache() from console, cacheList(Items) is called properly but the problem is, Items is always a empty array and its length is always 0.
Probably i am not using Promise resolve properly. Please help Thank you