Hide the Base map switch when there's no offline map to switch to
CI / check (push) Successful in 46s
CI / deploy (push) Successful in 19s

The Online/Offline sidebar icon was always shown, even on an install with
no data/offline.pmtiles — offering a choice that can't actually work.
LayerToggleControl#remove() removes the icon (and hides its panel) once the
existing /api/offline-tiles check confirms there's nothing to switch to; a
transient failure of that same-origin check leaves the switch as is rather
than guessing, since that's the request itself failing, not evidence
offline tiles are unavailable.
This commit is contained in:
randy committed 2026-09-07 17:32:05 +02:00
1 parent fd8852c0e1
commit 481233c232
2 files changed
+25 -6

No files matched your search

+20 -3
View File
@@ -101,6 +101,14 @@ class LayerToggleControl {
button.classList.toggle("active", button.dataset.style === name);
});
}
// Called once it's known there's no offline map to switch to (see the
// /api/offline-tiles check below) — an Online/Offline switch doesn't make
// sense to show at all when Offline was never going to work.
remove() {
this._container.remove();
this._panel.hidden = true;
}
}
const map = new MaplibreMap({
@@ -145,12 +153,21 @@ function applyStyle(name) {
}
// If a local PMTiles file is present, prefer it as the default style — its
// presence signals the app is meant to be used offline here.
// presence signals the app is meant to be used offline here. If one isn't,
// the Online/Offline switch itself doesn't make sense (there's nothing to
// switch to), so it's removed entirely rather than left sitting there
// offering a choice that can't work.
fetch("/api/offline-tiles")
.then((response) => response.json())
.then(({ available }) => {
if (available) applyStyle("offline");
if (available) {
applyStyle("offline");
} else {
layerToggle.remove();
}
})
.catch(() => {
// Backend check failed; keep the online style.
// Backend check failed; keep the online style and leave the switch as
// is rather than guessing — this is the same-origin request itself
// failing, not evidence offline tiles are actually unavailable.
});