diff --git a/client/src/components/Map/offlineBasemap.test.ts b/client/src/components/Map/offlineBasemap.test.ts index 665d59b84..f5dcdc4f2 100644 --- a/client/src/components/Map/offlineBasemap.test.ts +++ b/client/src/components/Map/offlineBasemap.test.ts @@ -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); diff --git a/client/src/components/Map/offlineBasemap.ts b/client/src/components/Map/offlineBasemap.ts index 3313dd1a9..a5aafc52d 100644 --- a/client/src/components/Map/offlineBasemap.ts +++ b/client/src/components/Map/offlineBasemap.ts @@ -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)); } diff --git a/client/src/components/Map/useEffectiveMapEngine.test.ts b/client/src/components/Map/useEffectiveMapEngine.test.ts index d363791fa..5e9f6227b 100644 --- a/client/src/components/Map/useEffectiveMapEngine.test.ts +++ b/client/src/components/Map/useEffectiveMapEngine.test.ts @@ -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')); diff --git a/client/src/sync/glTileCache.ts b/client/src/sync/glTileCache.ts index cb50a6c58..6ab0bc60f 100644 --- a/client/src/sync/glTileCache.ts +++ b/client/src/sync/glTileCache.ts @@ -23,7 +23,7 @@ export const GL_TILE_CACHE = 'map-tiles-gl'; * service worker by its source text, so it must not reach anything outside its * own body: the host list stays inline. */ -export function isGlRasterTileRequest({ url, request }: { url: URL; request: Request }): boolean { +export function isGlRasterTileRequest({ url, request }: { url: URL; request: Pick }): boolean { if (request.mode !== 'cors' || url.protocol !== 'https:') return false; const hosts = [ 'basemaps.cartocdn.com', @@ -38,3 +38,13 @@ export function isGlRasterTileRequest({ url, request }: { url: URL; request: Req const host = url.hostname.toLowerCase(); return hosts.some((entry) => (entry.startsWith('*.') ? host.endsWith(entry.slice(1)) : host === entry)); } + +/** Whether prefetch and the service worker can keep readable GL tiles for this template. */ +export function hasGlRasterCache(template: string): boolean { + try { + const url = new URL(template.replaceAll('{s}', 'a')); + return isGlRasterTileRequest({ url, request: { mode: 'cors' } }); + } catch { + return false; + } +} diff --git a/client/src/sync/tilePrefetcher.ts b/client/src/sync/tilePrefetcher.ts index e0cf4ff22..5fda1cfef 100644 --- a/client/src/sync/tilePrefetcher.ts +++ b/client/src/sync/tilePrefetcher.ts @@ -33,7 +33,7 @@ import { clearVectorCache, prefetchStyleAssets, prefetchVectorForPlaces } from ' import { isNativeIos } from '../native/platform' import { isIosDevice } from '../utils/iosDevice' import { openCacheStorage } from './openCacheStorage' -import { GL_TILE_CACHE, isGlRasterTileRequest } from './glTileCache' +import { GL_TILE_CACHE, hasGlRasterCache } from './glTileCache' import { useSettingsStore } from '../store/settingsStore' import { MAPLIBRE_FROM_TEMPLATE } from '@trek/shared' @@ -308,14 +308,7 @@ async function openTileCache(cacheName: string = TILE_CACHE): Promise