Repository navigation
Help bundling v6 for local use (double-click & open)? (worker issue) #8225
Replies: 5 comments 3 replies
|
I just stumbled across this when trying to update a local script that generates images for my app's legend. |
|
The As a quick check, serve the generated folder over localhost instead of double-clicking it: python3 -m http.server 8000 -d buildThen open |
|
Maybe use a bundler like rollup to repack it somehow, much like the build step that existed in v5... |
|
@larsmaxfield the worker file itself imports a sibling chunk (maplibre-gl-shared.mjs) as a genuine runtime import, that's why the vite docs specifically say to use ?worker&url instead of plain ?url.. plain url emits the worker standalone and it breaks on its first import since the sibling isn't there. so even if you get the entry worker script loaded via blob, the browser then tries to resolve that sibling import relative to wherever it thinks the worker "lives," and that's where bigger thing though: v6 release notes call this out directly.. the old CSP-friendly bundle from v5 (the one that let everything run through blob: with worker-src blob:) was dropped on purpose, because v6 moved to loading the worker as a real url instead. that's the whole reason v5 could do the "single html file, double click, works offline" trick and v6 structurally can't anymore. it's not that nobody's found the right vite flag yet, it's that the v5 approach was intentionally removed. on top of that, constructing a Worker from a so the "serve it over localhost" suggestion above isn't a workaround someone gave up and settled for, it's actually the correct answer given how v6 is built now. if double-click-and-open is a hard requirement, i'd either pin to v5 (works exactly as before) or ship a tiny local launcher alongside the html that spins up a one-line http server and opens the browser to it, which gets you the same "just double click a file" ux for the end user without touching |
|
Your blob approach is nearly there. The Chromium error comes from what maplibre does with your blob URL, not from the blob itself. Adding a fragment that ends in setWorkerUrl(URL.createObjectURL(blob) + '#maplibre-gl-worker.cjs');Why only Chromium fails
When the URL ends in I couldn't test Firefox, so I can't confirm why it worked there. My guess is that it doesn't report your blob as cross-origin and so skips step 2. About the earlier replies: Chromium doesn't block every worker on SvelteKit setup without the base64 paste step
"scripts": {
"build:worker": "esbuild node_modules/maplibre-gl/dist/maplibre-gl-worker.mjs --bundle --format=iife --minify --outfile=src/lib/maplibre-gl-worker.iife.js",
"build": "npm run build:worker && vite build"
}
import { setWorkerUrl } from 'maplibre-gl';
import workerCode from './maplibre-gl-worker.iife.js?raw';
if (typeof window !== 'undefined') {
const blobUrl = URL.createObjectURL(new Blob([workerCode], { type: 'text/javascript' }));
// A URL ending in ".cjs" makes maplibre start a classic worker instead of an ESM import wrapper
setWorkerUrl(blobUrl + '#maplibre-gl-worker.cjs');
}
What I tested
Limits
A supported way to ask for a classic worker would make this less fragile. It would need to be more than treating |
Uh oh!
There was an error while loading. Please reload this page.
Has anyone been successful in bundling v6 for local, offline use?
With v5 and SvelteKit I can bundle "inline" to include all JavaScript and CSS into a single HTML file. We give that HTML file to users who prefer to run things locally (double-click and open).
With v6 I get:
The worker URL is set with:
When I attempt to read the worker file as a raw string and feed its blob URL, I get:
I have little experience bundling and handling these errors, so I might be missing something obvious.
All reactions