From c97f7601532374f82edc0f76c63de80ffeb95a5b Mon Sep 17 00:00:00 2001 From: Tobias Gesellchen Date: Sun, 7 Jun 2026 11:00:22 +0200 Subject: [PATCH] fix(admin): only auto-discover on load when no devices are known (refs #451) The admin console ran a full discovery sweep on every page load whenever discovery was enabled (DOMContentLoaded -> triggerDiscovery). With devices already in the datastore, that re-probed every host (including offline ones) on each visit, which felt slow and surprising. Gate the on-load sweep on a cold start only: fetch the cached device list first, and trigger discovery just when it is empty. With devices known, rely on the cached list, the periodic sweep, and the explicit Discover button. fetchDevices now returns the device count for that check. Co-Authored-By: Claude Opus 4.8 (1M context) --- pkg/service/handlers/web/js/script.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/service/handlers/web/js/script.js b/pkg/service/handlers/web/js/script.js index bd15bf5..bd560ef 100644 --- a/pkg/service/handlers/web/js/script.js +++ b/pkg/service/handlers/web/js/script.js @@ -588,8 +588,11 @@ async function fetchDevices() { devices.forEach((d) => updateDeviceInfo(d.device_id, d.ip_address)); fetchSpotifyStatus(); } + + return devices.length; } catch (error) { document.getElementById("device-list").textContent = "Error loading devices: " + error; + return 0; } } @@ -1719,11 +1722,15 @@ function formatXML(xml) { document.addEventListener("DOMContentLoaded", async () => { const cfg = await fetchSettings(); - if (cfg?.discovery_enabled !== false) { + fetchVersion(); + const deviceCount = await fetchDevices(); + // Only sweep automatically on a cold start (no devices known yet). When + // devices are already in the store, rely on the cached list plus the + // periodic sweep and the explicit Discover button — re-probing offline + // speakers on every admin page load was slow and surprising. + if (deviceCount === 0 && cfg?.discovery_enabled !== false) { triggerDiscovery(); } - fetchVersion(); - await fetchDevices(); const hash = window.location.hash.slice(1); const [tabId, extra] = hash.split('?');