@amaui/utils
Version:
102 lines (83 loc) • 3.82 kB
JavaScript
import isEnvironment from './isEnvironment';
import isValid from './isValid';
import getObjectValue from './getObjectValue'; // In your specific app, make sure you import your own app's package.json,
// and set on window.AMAUI.app = { version: packageJson.version }, so checkAppVersion can get your
// app's actual version value instead of amaui-utils package.json version added atm
// meta.json file for this method to be used
// should be generated during build with,
// the package.json / app version inside of it
// so we can use it to compare it with local value
export const getMeta = async () => {
try {
var _AMAUI;
if (((_AMAUI = window.AMAUI) === null || _AMAUI === void 0 ? void 0 : _AMAUI.env) === 'test') {
var _AMAUI2, _AMAUI2$test, _AMAUI2$test$getMeta;
if ((_AMAUI2 = window.AMAUI) !== null && _AMAUI2 !== void 0 && (_AMAUI2$test = _AMAUI2.test) !== null && _AMAUI2$test !== void 0 && (_AMAUI2$test$getMeta = _AMAUI2$test.getMeta) !== null && _AMAUI2$test$getMeta !== void 0 && _AMAUI2$test$getMeta.return) return window.AMAUI.test.getMeta.return;
}
const headers = {
'Pragma': 'no-cache',
'Cache-Control': 'no-store'
};
let meta = await fetch('/meta.json', {
headers
});
meta = await meta.json();
return meta;
} catch (error) {
console.error('Get meta: ', error);
}
};
export const refreshCacheAndReload = async function () {
let reload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
console.log("Clearing all app cache + reloading.", reload);
if (caches) {
const names = await caches.keys();
for (const name of names) await caches.delete(name);
} // Delete browser cache + hard reload
if (reload) window.location.reload();
}; // If your app gets buggy and nothing is rendered
// in the root div of your app, in a use case where it can happen,
// use this method to check for rendered app html children elements
// and if none are there clean app cache + app reload
export const checkRoot = function (rootId) {
let timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 400;
let reload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
return new Promise(resolve => {
setTimeout(async () => {
var _root$children;
console.log('Root checking...');
const root = document.getElementById(rootId || 'root');
if (!(root !== null && root !== void 0 && (_root$children = root.children) !== null && _root$children !== void 0 && _root$children.length)) {
await refreshCacheAndReload(reload);
return resolve(false);
}
return resolve(true);
}, timeout);
});
};
const retriesDefault = 5;
const checkAppVersion = async function () {
let retries = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : retriesDefault;
let reload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
if (isEnvironment('browser')) {
const meta = await getMeta();
if (!meta && retries > 0) {
console.log('No meta, retry: ', retriesDefault - retries);
return checkAppVersion(--retries, reload);
}
const versionLatest = meta === null || meta === void 0 ? void 0 : meta.version;
const versionCurrent = getObjectValue(window, 'AMAUI.app.version') || '1.0.0';
const isPreviousVersion = !versionLatest || isValid('semver-compare', undefined, {
valueA: versionLatest,
valueB: versionCurrent,
operator: 'greater-than'
});
if (isPreviousVersion) {
console.log("A new app version ".concat(versionLatest, " exists. App about to clean cache and refresh."));
await refreshCacheAndReload(reload);
return false;
}
return true;
}
};
export default checkAppVersion;