Я сделал следующую функцию, чтобы проверить доступное хранилище кеша для предотвращения ошибок "Uncaught (в обещании) DOMException: Quota превышен"
function isCacheStorageSpaceAvailable()
{
var percentLimit = 90; // % percent of cache storage space that can be used
var percentUsed = 0;
if ('storage' in navigator && 'estimate' in navigator.storage)
{
navigator.storage.estimate()
.then(function(estimate){
percentUsed = ( estimate.usage/estimate.quota ) * 100; // used storage space in percent
});
if (percentUsed < percentLimit)
{
return true;
}
else
{
console.log("Cache storage space not available, allowed storage space: " + percentLimit + "%, used storage space: " + percentUsed + "%");
return false;
}
}
else
{
return true;
}
}
Где я должен использовать вышеуказанную функцию в следующем коде для работы
var offlineFundamentals = [
'/',
'/offline.html',
];
//Add core website files to cache during serviceworker installation
var updateStaticCache = function() {
return caches.open(version + 'fundamentals').then(function(cache) {
return Promise.all(offlineFundamentals.map(function(value) {
var request = new Request(value);
var url = new URL(request.url);
if (url.origin != location.origin) {
request = new Request(value, {mode: 'no-cors'});
}
return fetch(request).then(function(response) {
var cachedCopy = response.clone();
return cache.put(request, cachedCopy);
});
}))
})
};
Я плохо разбираюсь в программировании рабочих, мне нужна помощь.