I am working on a webpage that displays a message when the user is offline. However, I am facing an issue with my service worker while trying to cache the page. The Chrome console always throws this error:
service-worker.js?v=1:1 Uncaught (in promise) DOMException: Quota exceeded. Promise rejected (async) addEventListener.event @ service-worker.js?v=1:10
Here is the code snippet for service worker registration:
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./assets/app/js/service-worker.js?v=1').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});}
Below is the script for the service worker:
'use strict';
var cacheVersion = 1;
var currentCache = {
offline: 'offline-cache' + cacheVersion
};
var offlineUrl = '../../../offline.html';
this.addEventListener('install', event => {
event.waitUntil(
caches.open(currentCache.offline).then(function (cache) {
return cache.addAll([
offlineUrl
]);
})
);
});
this.addEventListener('fetch', event => {
if (event.request.mode === 'navigate' || (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) {
event.respondWith(
fetch(event.request.url).catch(error => {
return caches.match(offlineUrl);
})
);
}
else {
event.respondWith(caches.match(event.request)
.then(function (response) {
return response || fetch(event.request);
})
);
}
});
The content of offline.html:
<div> offline test </div>
I have tried deleting all caches but still receive the "Quota exceeded" error. Any suggestions or solutions? Thank you.