Skip to content

fix: set MapLibre CSP worker URL to prevent Vite production build errors - #8

Merged
morehawes merged 1 commit into
masterfrom
fix/csp-worker
Apr 2, 2026
Merged

fix: set MapLibre CSP worker URL to prevent Vite production build errors#8
morehawes merged 1 commit into
masterfrom
fix/csp-worker

Conversation

@morehawes

Copy link
Copy Markdown
Contributor

Problem

Consuming apps using @ogis/navigator with Vite hit a runtime ReferenceError in production builds (mo is not defined). Vite's minifier mangles variable names, but MapLibre serialises function bodies into inline worker blobs at runtime — the worker still references the original (pre-minified) names, causing a crash on first tile load.

This only affects production builds; dev mode works fine.

Fix

Call maplibregl.setWorkerUrl() with MapLibre's pre-built CSP worker at module load time in useMap.js. The CSP worker is a standalone, self-contained file — Vite emits it as a separate asset with proper URL handling, so minification no longer affects it.

import cspWorkerUrl from 'maplibre-gl/dist/maplibre-gl-csp-worker.js?url';
maplibregl.setWorkerUrl(cspWorkerUrl);

This is applied once at module initialisation, before any map instance is created. Consuming apps no longer need to apply this workaround themselves.

Testing

All 171 unit tests pass.

Closes #7

Call maplibregl.setWorkerUrl() with the pre-built CSP worker at module
load time in useMap.js. This prevents Vite's production minifier from
mangling variable names that MapLibre serialises into inline worker
blobs, which caused 'mo is not defined' ReferenceErrors at runtime.

Consuming apps no longer need to apply this workaround themselves.

Closes #7

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@morehawes
morehawes merged commit b66df23 into master Apr 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle MapLibre GL CSP worker setup to prevent Vite production build errors

1 participant