Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions ui/src/renderers/leaflet/__tests__/maplibreLoader.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
7 changes: 5 additions & 2 deletions ui/src/renderers/leaflet/leafletRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions ui/src/renderers/leaflet/maplibreLoader.ts
Original file line number Diff line number Diff line change
@@ -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<MaplibreModule> {
const maplibregl = await import("maplibre-gl");
if (maplibregl.getWorkerUrl() !== workerUrl) {
maplibregl.setWorkerUrl(workerUrl);
}
return maplibregl;
}
Loading