bd-chapril/bdchapril.js
2024-12-22 08:50:58 +01:00

79 lines
No EOL
2.8 KiB
JavaScript

'use strict';
const checkCacheFunction = () => {
if ('applicationCache' in window) {
try {
var webappCache = window.applicationCache;
const loaded = () => {
//var h1El = document.querySelector("h1");
var connectionStatus = ((navigator.onLine) ? 'online' : 'offline');
//h1El.textContent = h1El.textContent + " - currently: " + connectionStatus;
switch(webappCache.status)
{
case 0:
console.log("Cache status: Uncached.");
break;
case 1:
console.log("Cache status: Idle.");
break;
case 2:
console.log("Cache status: Checking.");
break;
case 3:
console.log("Cache status: Downloading.");
break;
case 4:
console.log("Cache status: Update ready.");
break;
case 5:
console.log("Cache status: Obsolete.");
break;
}
};
const updateCache = () => {
webappCache.swapCache();
console.log("Cache has been updated due to a change found in the manifest.");
};
const errorCache = () => {
console.log("You're either offline or something has gone horribly wrong.");
};
window.addEventListener("load", loaded, false);
webappCache.addEventListener("updateready", updateCache, false);
webappCache.addEventListener("error", errorCache, false);
} catch (error) {
console.error(`Cache check failed with ${error}.`);
}
} else {
console.warn("Application caches are not supported!");
}
};
const registerServiceWorker = async () => {
if ('serviceWorker' in navigator) {
try {
const registration = await navigator.serviceWorker.register(
'sw.js',
{
scope: './',
}
);
if (registration.installing) {
console.log('Service worker installing.');
} else if (registration.waiting) {
console.log('Service worker installed.');
} else if (registration.active) {
console.log('Service worker active.');
}
} catch (error) {
console.error(`Registration failed with ${error}.`);
}
} else {
console.warn("Service workers are not supported!");
}
};
checkCacheFunction();
registerServiceWorker();