mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26:47 +00:00
b401da2264
CARTO started stamping "API KEY REQUIRED" across every keyless basemap tile on 26.08.2026. #2060 added a key field as the immediate answer, but it leaves a self-hosted instance needing an email exchange with CARTO before its maps look right, and every default in the client still pointed at CARTO. Default to OpenFreeMap Positron instead: no key, no account, no request limit, commercial use allowed, attribution required. OpenFreeMap serves vector tiles only, so the Leaflet basemap is a MapLibre style document rather than an XYZ template. maplibre-gl-leaflet hangs a GL canvas into Leaflet's own tile pane, which leaves everything above it alone - markers, GeoJSON, clusters, plugin layers, the custom panes, and the click handling, since Leaflet's CSS already puts pointer-events:none on that layer. maplibre-gl still arrives only through a dynamic import, so no chunk that draws a map grows by a megabyte. Switched: the trip planner, the MapView fallback every preview goes through, the journey map, collections, the atlas, the shared trip page and the offline tile prefetch. OpenFreeMap Positron and Bright join the tile presets in settings, the admin defaults and both mobile screens. A CARTO template that is already saved keeps working when a key backs it - that stays a supported choice. Without a key it is not drawn at all: resolveTileUrl falls back to the default rather than to a watermark, so an instance that upgrades with one in its settings sees the new basemap immediately and needs no migration. The stored value is left as it is, so the Map settings tab still shows what was chosen and the warning underneath it explains why - one sentence appended, in all 23 locales. OpenFreeMap asks for a credit of its own; OpenStreetMap alone under its tiles is wrong and a licence problem. attributionForTile() derives it from the URL rather than from a flag at each call site, and the shared trip map, which carried no attribution at all, now gets one. Offline maps keep working and get cheaper. A vector style is four things instead of one, so sync/glPrefetcher.ts walks the style document, its sprite, the Latin glyph ranges of each font stack it uses, the TileJSON - the planet path is versioned, so guessing it answers 200 with an empty body - and then the .pbf tiles. Over the Berlin bounding box TREK computes, raster z10-16 is 2237 requests and 34.6 MB and gives up above z16; vector z10-14 is 177 requests and 32.1 MB and stays sharp above that, because MapLibre scales the z14 tile rather than asking for another. The Workbox rule for tiles.openfreemap.org moves to CacheFirst and shares the prefetcher's cache, so browsing tops a downloaded region up instead of filling a second copy beside it - a versioned tile path can never be stale. Clearing offline maps clears it too. The atlas loses its second raster layer. It existed to warm the HTTP cache of neighbouring zoom levels, which a vector basemap does not need; a second one would only cost a second WebGL context. OpenFreeMap has no label-free style, so its labels are switched off on the live map instead - they are all symbol layers, while borders and coastlines are line and fill - re-armed on style.load so a theme switch keeps them off. Theme changes call setStyle in place rather than rebuilding the layer: a swap per toggle would leak a WebGL context, and browsers cap those in the low teens.
116 lines
5.0 KiB
TypeScript
116 lines
5.0 KiB
TypeScript
/**
|
|
* OpenStreetMap has not needed the a/b/c.tile.openstreetmap.org subdomains
|
|
* since 2022 — tile.openstreetmap.org serves the whole grid on its own — and
|
|
* d.tile.openstreetmap.org has meanwhile lost its DNS record entirely. A
|
|
* template that still carries the `{s}` placeholder therefore depends on which
|
|
* letters the renderer substitutes: everything that lands on `d` fails with
|
|
* ERR_NAME_NOT_RESOLVED, which is console noise plus holes in the offline tile
|
|
* cache (#1733).
|
|
*
|
|
* The presets ship the single-host form now, but a template the user (or an
|
|
* admin default) saved earlier is still in the database. Rewriting it on read
|
|
* fixes those instances without a migration; the settings store rewrites it on
|
|
* save as well, so a legacy URL typed by hand converges on the same host
|
|
* instead of coming back on the next load. Other providers are left untouched.
|
|
*/
|
|
|
|
/** `{s}.` or a bare shard letter in front of tile.openstreetmap.org. */
|
|
const OSM_SHARD = /^((?:https?:)?\/\/)(?:\{s\}|[a-d])\.tile\.openstreetmap\.org(?=[/:]|$)/i
|
|
|
|
/** Collapse a sharded OSM tile template onto the single supported host. */
|
|
export function normalizeTileUrl(url: string): string {
|
|
if (!url) return url
|
|
return url.replace(OSM_SHARD, '$1tile.openstreetmap.org')
|
|
}
|
|
|
|
/**
|
|
* CARTO started watermarking keyless basemap tiles on 26.08.2026 (#2054). The
|
|
* key rides along as a `?key=` query parameter, which every template engine in
|
|
* the client passes through untouched, so it is appended here rather than baked
|
|
* into the stored template: a saved URL stays portable and survives a key
|
|
* change. Only CARTO hosts are touched; OSM and self-hosted templates are not.
|
|
*/
|
|
const CARTO_HOST = /^(?:\{s\}|[a-d])?\.?basemaps\.cartocdn\.com$/i
|
|
|
|
function templateHost(url: string): string {
|
|
return url.replace(/^\w*:?\/\//, '').split(/[/?#]/)[0]
|
|
}
|
|
|
|
export function withTileApiKey(url: string, key?: string | null): string {
|
|
if (!url || !key) return url
|
|
if (!CARTO_HOST.test(templateHost(url))) return url
|
|
if (/[?&]key=/.test(url)) return url
|
|
return `${url}${url.includes('?') ? '&' : '?'}key=${encodeURIComponent(key)}`
|
|
}
|
|
|
|
/** Keeps the key out of anything we persist: stored templates, book documents. */
|
|
export function stripTileApiKey(url: string): string {
|
|
if (!url || !/[?&]key=/.test(url)) return url
|
|
return url.replace(/([?&])key=[^&]*&?/, '$1').replace(/[?&]$/, '')
|
|
}
|
|
|
|
/**
|
|
* A CARTO template with no key behind it. Those tiles come back with "API KEY
|
|
* REQUIRED" burned into them since 26.08.2026 (#2054), which is worse than any
|
|
* basemap, so they are not drawn at all — the map falls back to the app default
|
|
* until a key is entered.
|
|
*
|
|
* Checked on the resolved URL rather than on the stored template, because that
|
|
* is where the key has been appended: one rule, and it cannot disagree with
|
|
* itself between call sites. The stored setting is deliberately left alone, so
|
|
* the Map settings tab still shows what the user picked and its existing warning
|
|
* still explains why.
|
|
*/
|
|
function isKeylessCarto(url: string): boolean {
|
|
return CARTO_HOST.test(templateHost(url)) && !/[?&]key=/.test(url)
|
|
}
|
|
|
|
/**
|
|
* A blank template means "not configured", not "no tiles": the settings
|
|
* previews save an empty string and would otherwise render grey.
|
|
*/
|
|
export function resolveTileUrl(template: string | null | undefined, fallback: string, cartoKey?: string | null): string {
|
|
const chosen = withTileApiKey(normalizeTileUrl(template?.trim() || fallback), cartoKey)
|
|
return isKeylessCarto(chosen) ? fallback : chosen
|
|
}
|
|
|
|
/**
|
|
* The basemap a map should draw, in the one shape every caller can act on.
|
|
*
|
|
* Two kinds exist since the move off CARTO. A raster template goes into a
|
|
* Leaflet TileLayer as before; a vector style is a MapLibre style document that
|
|
* Leaflet cannot render on its own and that VectorBasemap hangs into the tile
|
|
* pane instead. Callers switch on `kind` rather than sniffing the URL, so a
|
|
* self-hosted raster template keeps working exactly as it did.
|
|
*/
|
|
export type Basemap =
|
|
| { kind: 'raster'; url: string }
|
|
| { kind: 'vector'; style: string }
|
|
|
|
/** A MapLibre style document rather than a `{z}/{x}/{y}` tile template. */
|
|
export function isVectorStyle(url: string | null | undefined): boolean {
|
|
if (!url) return false
|
|
const u = url.trim()
|
|
if (!u) return false
|
|
// A tile template always carries its placeholders; a style URL never does.
|
|
if (/\{[zxy]\}/i.test(u)) return false
|
|
return /^https?:\/\//i.test(u) || u.startsWith('mapbox://')
|
|
}
|
|
|
|
/**
|
|
* What a map should draw, given the user's template and the app's default.
|
|
*
|
|
* `template` is the user's own choice and wins whenever they made one.
|
|
* `fallback` is the app default and is a vector style now, so an unconfigured
|
|
* map — and one left on a keyless CARTO template — gets OpenFreeMap.
|
|
*/
|
|
export function resolveBasemap(
|
|
template: string | null | undefined,
|
|
fallback: string,
|
|
cartoKey?: string | null,
|
|
): Basemap {
|
|
const chosen = resolveTileUrl(template, fallback, cartoKey)
|
|
if (isVectorStyle(chosen)) return { kind: 'vector', style: chosen }
|
|
return { kind: 'raster', url: chosen }
|
|
}
|