@amaui/utils
Version:
86 lines (85 loc) • 3.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkRoot = exports.refreshCacheAndReload = exports.getMeta = void 0;
const isEnvironment_1 = __importDefault(require("./isEnvironment"));
const isValid_1 = __importDefault(require("./isValid"));
const getObjectValue_1 = __importDefault(require("./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
const getMeta = async () => {
var _a, _b, _c, _d;
try {
if (((_a = window.AMAUI) === null || _a === void 0 ? void 0 : _a.env) === 'test') {
if ((_d = (_c = (_b = window.AMAUI) === null || _b === void 0 ? void 0 : _b.test) === null || _c === void 0 ? void 0 : _c.getMeta) === null || _d === void 0 ? void 0 : _d.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);
}
};
exports.getMeta = getMeta;
const refreshCacheAndReload = async (reload = 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();
};
exports.refreshCacheAndReload = refreshCacheAndReload;
// 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
const checkRoot = (rootId, timeout = 400, reload = true) => new Promise(resolve => {
setTimeout(async () => {
var _a;
console.log('Root checking...');
const root = document.getElementById(rootId || 'root');
if (!((_a = root === null || root === void 0 ? void 0 : root.children) === null || _a === void 0 ? void 0 : _a.length)) {
await (0, exports.refreshCacheAndReload)(reload);
return resolve(false);
}
return resolve(true);
}, timeout);
});
exports.checkRoot = checkRoot;
const retriesDefault = 5;
const checkAppVersion = async (retries = retriesDefault, reload = true) => {
if ((0, isEnvironment_1.default)('browser')) {
const meta = await (0, exports.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 = (0, getObjectValue_1.default)(window, 'AMAUI.app.version') || '1.0.0';
const isPreviousVersion = !versionLatest || (0, isValid_1.default)('semver-compare', undefined, { valueA: versionLatest, valueB: versionCurrent, operator: 'greater-than' });
if (isPreviousVersion) {
console.log(`A new app version ${versionLatest} exists. App about to clean cache and refresh.`);
await (0, exports.refreshCacheAndReload)(reload);
return false;
}
return true;
}
};
exports.default = checkAppVersion;