fix(map): use stored basemaps for uncached GL raster hosts

Share the service-worker raster match with prefetch and the offline renderer. Keep an allowed custom host online and switch to the stored basemap offline when its tiles are outside the readable cache.
This commit is contained in:
Maurice
2026-12-06 16:04:31 +01:00
parent f394818f40
commit dcbabe998b
5 changed files with 39 additions and 16 deletions
@@ -33,12 +33,14 @@ describe('offlineBasemap', () => {
expect(leafletWorksOffline('https://tile.openstreetmap.org/{z}/{x}/{y}.png', false)).toBe(false);
});
it('draws a MapLibre raster template offline where Leaflet would keep the same tiles', () => {
it('keeps only MapLibre raster templates supported by the CORS cache', () => {
const own = mapLibreBasemap('trek:template', 'https://tiles.example.com/{z}/{x}/{y}.png', false, [
'tiles.example.com',
]);
const osm = mapLibreBasemap('trek:template', 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', false, []);
expect(glWorksOffline('maplibre-gl', own.style)).toBe(true);
const cached = mapLibreBasemap('trek:template', 'https://tile.openstreetmap.de/{z}/{x}/{y}.png', false, []);
expect(glWorksOffline('maplibre-gl', cached.style)).toBe(true);
expect(glWorksOffline('maplibre-gl', own.style)).toBe(false);
// The OSM tile usage policy forbids storing its tiles, so the stored basemap stands in.
expect(glWorksOffline('maplibre-gl', osm.style)).toBe(false);
expect(glWorksOffline('mapbox-gl', own.style)).toBe(false);
+7 -4
View File
@@ -1,5 +1,6 @@
import { OFM_DARK, OFM_POSITRON } from '../../constants/mapDefaults';
import { isNativeIos } from '../../native/platform';
import { hasGlRasterCache } from '../../sync/glTileCache';
import { isOfflineForbiddenTemplate, isVectorStyle } from '../../utils/tileUrl';
import { isGlRasterStyle, type GlStyle } from './glBasemap';
import { normalizeStyleForProvider, type GlMapProvider } from './glProviders';
@@ -25,13 +26,15 @@ export function offlineStyleFor(dark: boolean): string {
/**
* Whether a GL map can be drawn offline as configured. Only MapLibre can, on
* one of the stored styles (another OpenFreeMap style lacks its sprite and
* glyphs offline) or on a raster template whose tiles the prefetch stored the
* way Leaflet's are; Mapbox has nothing stored to draw from. `style` is what the
* map draws: the slot's value, or the style a keyword resolved to.
* glyphs offline) or on a raster template the worker caches with CORS. Mapbox
* has nothing stored to draw from. `style` is the map's resolved style.
*/
export function glWorksOffline(provider: GlMapProvider, style: GlStyle | null | undefined): boolean {
if (provider !== 'maplibre-gl') return false;
if (isGlRasterStyle(style)) return leafletWorksOffline(style.metadata['trek:template']);
if (isGlRasterStyle(style)) {
const template = style.metadata['trek:template'];
return leafletWorksOffline(template) && hasGlRasterCache(template);
}
return OFFLINE_STYLES.includes(normalizeStyleForProvider('maplibre-gl', style));
}
@@ -264,6 +264,21 @@ describe('useEffectiveMapEngine: the MapLibre keywords', () => {
expect(result.current.pill).toBeNull();
});
it('FE-MAP-EFFENGINE-041: a custom GL raster host swaps offline and returns when online', () => {
settings({ map_provider: 'maplibre-gl', maplibre_style: 'trek:template' });
seedStore(useAuthStore, { mapExtraConnectHosts: ['tiles.example.com'] });
const { result, rerender } = renderHook(() => useEffectiveMapEngine(OWN_RASTER));
expect(result.current).toMatchObject({
engine: 'maplibre-gl',
styleOverride: OFM_POSITRON,
pill: { kind: 'stored' },
});
env.offline = false;
rerender();
expect(styleSource(result.current.styleOverride)?.tiles).toEqual([OWN_RASTER]);
expect(result.current.pill).toBeNull();
});
it('FE-MAP-EFFENGINE-038: OpenStreetMap, never stored, swaps to the stored basemap', () => {
settings({ map_provider: 'maplibre-gl', maplibre_style: 'trek:template' });
const { result } = renderHook(() => useEffectiveMapEngine('https://tile.openstreetmap.org/{z}/{x}/{y}.png'));