mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 18:54:17 +00:00
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:
@@ -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);
|
||||
|
||||
@@ -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'));
|
||||
|
||||
@@ -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<Request, 'mode'> }): 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Cache | nu
|
||||
*/
|
||||
function fetchedAsGl(template: string): boolean {
|
||||
const { map_provider: provider, maplibre_style: style } = useSettingsStore.getState().settings
|
||||
if (provider !== 'maplibre-gl' || style !== MAPLIBRE_FROM_TEMPLATE) return false
|
||||
try {
|
||||
const url = new URL(template.replace(/\{s\}/g, 'a'))
|
||||
// The service worker's own matcher, asked about a CORS request for this host.
|
||||
return isGlRasterTileRequest({ url, request: { mode: 'cors' } as Request })
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
return provider === 'maplibre-gl' && style === MAPLIBRE_FROM_TEMPLATE && hasGlRasterCache(template)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user