From b5b0bcc766b570071aa4a9df4ee418cd42749a6a Mon Sep 17 00:00:00 2001 From: "Jokob @NetAlertX" <96159884+jokob-sk@users.noreply.github.com> Date: Fri, 13 Mar 2026 12:52:22 +0000 Subject: [PATCH] work on stale cache #1554 --- front/deviceDetails.php | 4 ++-- front/js/cache.js | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/front/deviceDetails.php b/front/deviceDetails.php index ec597bb9..6c0a1180 100755 --- a/front/deviceDetails.php +++ b/front/deviceDetails.php @@ -299,7 +299,7 @@ function updateChevrons(currentMac) { showSpinner(); - cacheDevices().then(() => { + cacheDevices(true).then(() => { hideSpinner(); // Retry after re-caching @@ -507,7 +507,7 @@ function updateDevicePageName(mac) { if (mac != 'new' && (name === null|| owner === null)) { console.warn("Device not found in cache, retrying after re-cache:", mac); showSpinner(); - cacheDevices().then(() => { + cacheDevices(true).then(() => { hideSpinner(); // Retry after successful cache updateDevicePageName(mac); diff --git a/front/js/cache.js b/front/js/cache.js index 9b35ec83..f224f6f2 100644 --- a/front/js/cache.js +++ b/front/js/cache.js @@ -451,11 +451,23 @@ function getDevDataByMac(macAddress, dbColumn) { } // ----------------------------------------------------------------------------- -// Cache the devices as one JSON -function cacheDevices() +/** + * Fetches the full device list from table_devices.json and stores it in + * localStorage under CACHE_KEYS.DEVICES_ALL. + * + * On subsequent calls the fetch is skipped if the initFlag is already set, + * unless forceRefresh is true. Pass forceRefresh = true whenever the caller + * knows the cached list may be stale (e.g. a device was not found by MAC and + * the page needs to recover without a full clearCache()). + * + * @param {boolean} [forceRefresh=false] - When true, bypasses the initFlag + * guard and always fetches fresh data from the server. + * @returns {Promise} Resolves when the cache has been populated. + */ +function cacheDevices(forceRefresh = false) { return new Promise((resolve, reject) => { - if(getCache(CACHE_KEYS.initFlag('cacheDevices')) === "true") + if(!forceRefresh && getCache(CACHE_KEYS.initFlag('cacheDevices')) === "true") { // One-time migration: normalize legacy { data: [...] } wrapper to a plain array. // Old cache entries from prior versions stored the raw API envelope; re-write