fix(offline): raster tiles a GL map fetches get a service worker cache of their own

Leaflet loads a tile as an image without CORS and the prefetch fetches it
no-cors, so map-tiles holds opaque answers. A GL map reads tiles with a
CORS fetch and cannot use one: once Leaflet had drawn an OpenTopoMap tile,
the GL tour layer failed on the same URL, and a MapLibre template would
fail the same way on every prefetched tile.

A CORS request for a raster preset's tile now goes to map-tiles-gl, which
keeps only readable answers, ahead of the Leaflet rules; images and the
no-cors prefetch stay in map-tiles. The matcher is written into the
service worker by its source text, so its host list is inline, and a test
holds it to the hosts the CSP lets a GL map fetch. OpenHikingMap and
CyclOSM stay out, their usage policies allow no offline copy.

The prefetch takes exactly one way per user: a template MapLibre draws on
such a host is fetched with CORS into map-tiles-gl, at the density the map
asks for, everything else as before. Clearing offline maps drops both
caches, and the iOS trim keeps both within its budget.
This commit is contained in:
Maurice
2026-11-30 15:35:24 +01:00
parent 32879dd528
commit d39d544297
6 changed files with 266 additions and 14 deletions
+81
View File
@@ -0,0 +1,81 @@
/**
* The service worker rule for raster tiles a GL map fetches (FE-SYNC-GLTILECACHE-*).
*/
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { RASTER_TILE_HOSTS } from '@trek/shared';
import { describe, expect, it } from 'vitest';
import { GL_TILE_CACHE, isGlRasterTileRequest } from './glTileCache';
import { MAX_TILES } from './tilePrefetcher';
function ask(match: typeof isGlRasterTileRequest, url: string, mode: RequestMode) {
return match({ url: new URL(url), request: { mode } as Request });
}
/** The function as the service worker gets it: its source text, evaluated with nothing around it. */
const serialized = new Function(`return (${isGlRasterTileRequest.toString()})`)() as typeof isGlRasterTileRequest;
describe('isGlRasterTileRequest', () => {
it('FE-SYNC-GLTILECACHE-001: takes the CORS fetches of the cached raster presets', () => {
for (const url of [
'https://a.basemaps.cartocdn.com/light_all/5/16/10@2x.png?key=k',
'https://basemaps.cartocdn.com/light_all/5/16/10.png',
'https://tile.openstreetmap.org/5/16/10.png',
'https://tile.openstreetmap.de/5/16/10.png',
'https://tiles.stadiamaps.com/tiles/alidade_smooth/5/16/10.png',
// The GL tour layer that broke on Leaflet's opaque copies.
'https://b.tile.opentopomap.org/5/16/10.png',
]) {
expect(ask(isGlRasterTileRequest, url, 'cors'), url).toBe(true);
}
});
it("FE-SYNC-GLTILECACHE-002: leaves Leaflet's images and the no-cors prefetch to map-tiles", () => {
expect(ask(isGlRasterTileRequest, 'https://tile.openstreetmap.de/5/16/10.png', 'no-cors')).toBe(false);
expect(ask(isGlRasterTileRequest, 'https://tile.openstreetmap.de/5/16/10.png', 'same-origin')).toBe(false);
});
it('FE-SYNC-GLTILECACHE-003: never a host whose policy forbids an offline copy, a GCJ-02 host or a look-alike', () => {
for (const url of [
'https://tile.openmaps.fr/openhikingmap/5/16/10.png',
'https://a.tile-cyclosm.openstreetmap.fr/cyclosm/5/16/10.png',
'https://webrd01.is.autonavi.com/appmaptile?x=1&y=2&z=3',
'https://tile.openstreetmap.de.example.com/5/16/10.png',
'http://tile.openstreetmap.de/5/16/10.png',
]) {
expect(ask(isGlRasterTileRequest, url, 'cors'), url).toBe(false);
}
});
it('FE-SYNC-GLTILECACHE-004: reaches nothing outside its body, so the copy in the service worker matches alike', () => {
for (const url of ['https://tile.openstreetmap.de/5/16/10.png', 'https://tile.openmaps.fr/x/5/16/10.png']) {
for (const mode of ['cors', 'no-cors'] as const) {
expect(ask(serialized, url, mode)).toBe(ask(isGlRasterTileRequest, url, mode));
}
}
});
it('FE-SYNC-GLTILECACHE-005: every host in the inline list is one the CSP lets a GL map fetch', () => {
const inline = [...isGlRasterTileRequest.toString().matchAll(/'([^']+\.[a-z]+)'/g)].map((m) => m[1]);
expect(inline.length).toBeGreaterThan(0);
for (const host of inline) expect(RASTER_TILE_HOSTS, host).toContain(host);
});
});
describe('the map-tiles-gl rule in vite.config.js', () => {
const config = readFileSync(resolve(process.cwd(), 'vite.config.js'), 'utf8');
const rule = /urlPattern: isGlRasterTileRequest,[\s\S]*?cacheableResponse: \{ statuses: \[200\] \}/.exec(config)?.[0];
it('FE-SYNC-GLTILECACHE-006: keeps only readable answers, as many as the prefetch budget', () => {
expect(GL_TILE_CACHE).toBe('map-tiles-gl');
expect(rule).toBeDefined();
expect(rule).toContain('cacheName: GL_TILE_CACHE');
expect(rule).toContain(`maxEntries: ${MAX_TILES}`);
});
it('FE-SYNC-GLTILECACHE-007: comes before the Leaflet rules for the same hosts, since the first match wins', () => {
expect(config.indexOf('urlPattern: isGlRasterTileRequest')).toBeLessThan(config.indexOf('basemaps\\.cartocdn'));
});
});