From 605f244755e768cab36d68f85277ba051c36b742 Mon Sep 17 00:00:00 2001 From: Smith <13200576+smitt14ua@users.noreply.github.com> Date: Wed, 4 Nov 2026 04:04:45 +0200 Subject: [PATCH] fix(ui): ship the MapLibre worker so vector map layers render MapLibre 6 (bumped in #581) loads its worker from ./maplibre-gl-worker.mjs next to its own module. Vite bundles maplibre-gl into a hashed chunk and never emits that file, so the request falls through to index.html and every worker fails with "Worker failed to load". Raster layers still draw, but vector tiles are never parsed: Topo and Topo Dark show an empty background and the relief styles lose roads, buildings, water and labels. Import the worker with ?url so Vite emits it as an asset, and pass its URL to setWorkerUrl() before any map is created. Both maplibre-gl import sites now go through loadMaplibre(). --- .../leaflet/__tests__/maplibreLoader.test.ts | 38 +++++++++++++++++++ ui/src/renderers/leaflet/leafletRenderer.ts | 7 +++- ui/src/renderers/leaflet/maplibreLoader.ts | 20 ++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 ui/src/renderers/leaflet/__tests__/maplibreLoader.test.ts create mode 100644 ui/src/renderers/leaflet/maplibreLoader.ts diff --git a/ui/src/renderers/leaflet/__tests__/maplibreLoader.test.ts b/ui/src/renderers/leaflet/__tests__/maplibreLoader.test.ts new file mode 100644 index 000000000..e8fd6c4cb --- /dev/null +++ b/ui/src/renderers/leaflet/__tests__/maplibreLoader.test.ts @@ -0,0 +1,38 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; + +const state = vi.hoisted(() => ({ workerUrl: "" })); + +vi.mock("maplibre-gl/dist/maplibre-gl-worker.mjs?url", () => ({ + default: "/assets/maplibre-gl-worker-abc123.mjs", +})); + +vi.mock("maplibre-gl", () => ({ + getWorkerUrl: vi.fn(() => state.workerUrl), + setWorkerUrl: vi.fn((url: string) => { + state.workerUrl = url; + }), +})); + +import * as maplibregl from "maplibre-gl"; +import { loadMaplibre } from "../maplibreLoader"; + +describe("loadMaplibre", () => { + beforeEach(() => { + state.workerUrl = ""; + vi.mocked(maplibregl.setWorkerUrl).mockClear(); + }); + + it("points MapLibre at the worker file Vite emits", async () => { + const ml = await loadMaplibre(); + + expect(maplibregl.setWorkerUrl).toHaveBeenCalledWith("/assets/maplibre-gl-worker-abc123.mjs"); + expect(ml.getWorkerUrl()).toBe("/assets/maplibre-gl-worker-abc123.mjs"); + }); + + it("sets the worker URL only once across calls", async () => { + await loadMaplibre(); + await loadMaplibre(); + + expect(maplibregl.setWorkerUrl).toHaveBeenCalledTimes(1); + }); +}); diff --git a/ui/src/renderers/leaflet/leafletRenderer.ts b/ui/src/renderers/leaflet/leafletRenderer.ts index c4f46dfee..7b73b8543 100644 --- a/ui/src/renderers/leaflet/leafletRenderer.ts +++ b/ui/src/renderers/leaflet/leafletRenderer.ts @@ -24,6 +24,7 @@ import { createScaleControl } from "./leafletControls"; import { basePath } from "../../data/basePath"; import type { StyleCandidate } from "./leafletControls"; import { createGridLayer } from "./leafletGrid"; +import { loadMaplibre } from "./maplibreLoader"; import { ensureDefs, nextPatternId, @@ -312,7 +313,7 @@ export class LeafletRenderer implements MapRenderer { if (!(window as any)._pmtilesRegistered) { try { const { Protocol } = await import("pmtiles"); - const maplibregl = await import("maplibre-gl"); + const maplibregl = await loadMaplibre(); const protocol = new Protocol(); // Wrap PMTiles handler to resolve relative paths against app base maplibregl.addProtocol( @@ -389,6 +390,8 @@ export class LeafletRenderer implements MapRenderer { ]; const initialStyle = await fetchStyle(initialCandidate.url); + // Set the worker URL even if PMTiles registration above failed. + await loadMaplibre(); await import("@maplibre/maplibre-gl-leaflet"); const mlLayer = (L as any).maplibreGL({ style: initialStyle, @@ -1435,7 +1438,7 @@ export class LeafletRenderer implements MapRenderer { document.body.appendChild(div); fetchStyleFn(styleUrl) - .then((style) => import("maplibre-gl").then((maplibregl) => ({ style, maplibregl }))) + .then((style) => loadMaplibre().then((maplibregl) => ({ style, maplibregl }))) .then(({ style, maplibregl }) => { const mapOpts: any = { container: div, diff --git a/ui/src/renderers/leaflet/maplibreLoader.ts b/ui/src/renderers/leaflet/maplibreLoader.ts new file mode 100644 index 000000000..4490c901d --- /dev/null +++ b/ui/src/renderers/leaflet/maplibreLoader.ts @@ -0,0 +1,20 @@ +// MapLibre 6 loads its worker from `./maplibre-gl-worker.mjs` relative to its +// own module URL. Vite bundles maplibre-gl into a hashed chunk and does not +// emit that file, so the request falls through to the SPA's index.html and +// every worker fails ("Worker failed to load"). Raster layers still draw, but +// vector tiles (roads, buildings, labels) are never parsed. +// +// Importing the worker with `?url` makes Vite emit it as an asset and gives us +// its final URL, which we hand to MapLibre before any map is created. +import workerUrl from "maplibre-gl/dist/maplibre-gl-worker.mjs?url"; + +type MaplibreModule = typeof import("maplibre-gl"); + +/** Load maplibre-gl with its worker URL set. Use this instead of `import("maplibre-gl")`. */ +export async function loadMaplibre(): Promise { + const maplibregl = await import("maplibre-gl"); + if (maplibregl.getWorkerUrl() !== workerUrl) { + maplibregl.setWorkerUrl(workerUrl); + } + return maplibregl; +}