Hide the Base map switch when there's no offline map to switch to
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:
1 parent
fd8852c0e1
commit
481233c232
2 files changed
+25
-6
No files matched your search
@@ -59,9 +59,11 @@ view (`public/js/map.js`) offers two styles:
|
|||||||
|
|
||||||
`GET /api/offline-tiles` reports whether `data/offline.pmtiles` is
|
`GET /api/offline-tiles` reports whether `data/offline.pmtiles` is
|
||||||
present (`{"available": true|false}`); the frontend uses it once, on
|
present (`{"available": true|false}`); the frontend uses it once, on
|
||||||
load, to default to the offline style when local tile data exists —
|
load, to default to the offline style when local tile data exists.
|
||||||
otherwise it defaults to the online style. Either way, the layer
|
When it doesn't, `LayerToggleControl#remove()` (`public/js/map.js`)
|
||||||
toggle in the top-left of the map lets the user switch manually.
|
removes the Online/Offline sidebar icon entirely — a switch offering a
|
||||||
|
choice that can't work is worse than no switch at all — so it's only
|
||||||
|
ever shown on an install that actually has a PMTiles file to switch to.
|
||||||
|
|
||||||
PMTiles is a single, self-indexed file, which is why this needs no
|
PMTiles is a single, self-indexed file, which is why this needs no
|
||||||
tile-decoding logic on the server: `app.rb`'s `/tiles/offline.pmtiles`
|
tile-decoding logic on the server: `app.rb`'s `/tiles/offline.pmtiles`
|
||||||
|
|||||||
+20
-3
@@ -101,6 +101,14 @@ class LayerToggleControl {
|
|||||||
button.classList.toggle("active", button.dataset.style === name);
|
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({
|
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
|
// 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")
|
fetch("/api/offline-tiles")
|
||||||
.then((response) => response.json())
|
.then((response) => response.json())
|
||||||
.then(({ available }) => {
|
.then(({ available }) => {
|
||||||
if (available) applyStyle("offline");
|
if (available) {
|
||||||
|
applyStyle("offline");
|
||||||
|
} else {
|
||||||
|
layerToggle.remove();
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.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.
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user