fix(basemap): default to keyless OpenFreeMap

This commit is contained in:
Shadowbroker committed 2026-10-06 15:48:00 -06:00
1 parent 453010af1a
commit 4128a0ac7d
1 file changed
+25 -19
+25 -19
View File
@@ -1,54 +1,60 @@
/**
* MapLibre basemap styles on CARTO raster tiles. CARTO requires an API key
* (unkeyed tiles are watermarked); MaplibreViewer passes one from
* useBasemapConfig() via buildBasemapStyle(). The key is served by the
* backend at GET /api/basemap-config.
* ShadowBroker's zero-config basemap uses OpenFreeMap vector styles.
* Operators who configure CARTO_API_KEY keep the existing CARTO raster
* basemap path. This avoids CARTO's unkeyed "API KEY REQUIRED" watermark
* without taking the configured provider away from existing installs.
*/
export type BasemapTheme = 'dark' | 'light';
export const OPENFREEMAP_STYLE_URLS: Record<BasemapTheme, string> = {
dark: 'https://tiles.openfreemap.org/styles/dark',
light: 'https://tiles.openfreemap.org/styles/positron',
};
const CARTO_SUBDOMAINS = ['a', 'b', 'c', 'd'] as const;
const CARTO_RASTER_STYLE: Record<BasemapTheme, string> = {
dark: 'dark_all',
light: 'light_all',
};
const GLYPHS_URL = 'https://demotiles.maplibre.org/font/{fontstack}/{range}.pbf';
// Declared on the raster source so MapLibre's AttributionControl shows it
// even without the custom list in MaplibreViewer. Same markup as that list so
// the control de-duplicates instead of showing both.
export const OSM_ATTRIBUTION_HTML =
'<a href="https://www.openstreetmap.org/copyright" target="_blank" rel="noopener">© OpenStreetMap contributors</a>';
export const CARTO_ATTRIBUTION_HTML =
'<a href="https://carto.com/attribution" target="_blank" rel="noopener">CARTO</a>';
export const OPENFREEMAP_ATTRIBUTION_HTML =
'<a href="https://openfreemap.org" target="_blank" rel="noopener">OpenFreeMap</a> · <a href="https://openmaptiles.org" target="_blank" rel="noopener">OpenMapTiles</a>';
/** Tile URL templates for a CARTO raster style, keyed when a key is supplied. */
export function cartoTileUrls(theme: BasemapTheme, cartoApiKey?: string | null): string[] {
/** Tile URL templates for the optional CARTO raster style. */
export function cartoTileUrls(theme: BasemapTheme, cartoApiKey: string): string[] {
const style = CARTO_RASTER_STYLE[theme];
const key = (cartoApiKey || '').trim();
const query = key ? `?key=${encodeURIComponent(key)}` : '';
const key = cartoApiKey.trim();
return CARTO_SUBDOMAINS.map(
(s) => `https://${s}.basemaps.cartocdn.com/rastertiles/${style}/{z}/{x}/{y}@2x.png${query}`,
(s) =>
`https://${s}.basemaps.cartocdn.com/rastertiles/${style}/{z}/{x}/{y}@2x.png?key=${encodeURIComponent(key)}`,
);
}
/**
* Use OpenFreeMap by default. CARTO is retained as an opt-in compatibility
* path when an operator has already configured CARTO_API_KEY.
*/
export function buildBasemapStyle(theme: BasemapTheme, cartoApiKey?: string | null) {
const key = (cartoApiKey || '').trim();
if (!key) return OPENFREEMAP_STYLE_URLS[theme];
const sourceId = `carto-${theme}`;
return {
version: 8,
glyphs: GLYPHS_URL,
sources: {
[sourceId]: {
type: 'raster',
tiles: cartoTileUrls(theme, cartoApiKey),
tiles: cartoTileUrls(theme, key),
tileSize: 256,
attribution: `${OSM_ATTRIBUTION_HTML} ${CARTO_ATTRIBUTION_HTML}`,
},
},
layers: [
{ id: `${sourceId}-layer`, type: 'raster', source: sourceId, minzoom: 0, maxzoom: 22 },
{ id: 'imagery-ceiling', type: 'background', paint: { 'background-opacity': 0 } },
],
layers: [{ id: `${sourceId}-layer`, type: 'raster', source: sourceId, minzoom: 0, maxzoom: 22 }],
};
}