Skip to content

feat(web): support Expo Snack web preview - #4408

Merged
mfazekas merged 1 commit into
mainfrom
fix/web-snack
Oct 26, 2026
Merged

mfazekas merged 1 commit into
mainfrom
fix/web-snack

Conversation

@mfazekas

Copy link
Copy Markdown
Contributor

Description

Makes the web implementation load and run in Expo Snack's web preview, so docs examples can later link to a Snack.

  1. Snack's bundler has no CSS loader, and the static import 'mapbox-gl/dist/mapbox-gl.css' stopped the package from loading. The import is gone. When the web MapView mounts, it now injects mapbox-gl's CSS into one id-guarded <style> element, unless the page already has the CSS (the same .mapboxgl-canary check mapbox-gl uses). yarn generate inlines the CSS string into src/web/mapboxglCss.ts.
  2. Snack breaks mapbox-gl's built-in worker (_ is not defined). The new Mapbox.setWorkerUrl(url) sets mapboxgl.workerUrl and is a no-op on iOS and Android. It's documented in src/web/install.md.

Existing web apps behave the same, except that they no longer need a CSS loader.

Verified:

  • With webpack set up like snackager (no loaders), 10.3.11 fails with "Module parse failed" on mapbox-gl.css; this branch bundles.
  • Pasted that bundle into a Snack (no token). Without setWorkerUrl the worker throws _ is not defined. With it, both maps load with no worker errors, and there is one style element across two maps and a remount.
  • In the example web app, maps, controls, attribution and markers look the same as before, with one style element after several mounts.
  • Unit tests cover CSS injection idempotency and setWorkerUrl.

Checklist

  • I've read CONTRIBUTING.md
  • I updated the doc/other generated code with running yarn generate in the root folder
  • I have tested the new feature on /example app (web only).
    • In V11 mode/ios
    • In New Architecture mode/ios
    • In V11 mode/android
    • In New Architecture mode/android
  • I added/updated a sample - if a new feature was implemented (/example)

Screenshot OR Video

Component to reproduce the issue you're fixing

import { Platform } from 'react-native';
import Mapbox, { MapView, Camera } from '@rnmapbox/maps';
import mapboxgl from 'mapbox-gl';

if (Platform.OS === 'web') {
  const worker = `importScripts('https://api.mapbox.com/mapbox-gl-js/v${mapboxgl.version}/mapbox-gl-csp-worker.js');`;
  Mapbox.setWorkerUrl(
    URL.createObjectURL(new Blob([worker], { type: 'text/javascript' })),
  );
}

export default function App() {
  return (
    <MapView style={{ flex: 1 }}>
      <Camera centerCoordinate={[19.04, 47.5]} zoomLevel={10} />
    </MapView>
  );
}

Snack's bundler has no CSS loader, so the web MapView now injects
mapbox-gl's CSS (inlined into src/web/mapboxglCss.ts by yarn generate)
unless the page already has it. Snack also breaks mapbox-gl's built-in
worker, so add Mapbox.setWorkerUrl to point it at the CSP worker build;
it's a no-op on iOS and Android.
@mfazekas
mfazekas merged commit 13a20d4 into main Oct 26, 2026
10 checks passed
@mfazekas
mfazekas deleted the fix/web-snack branch October 26, 2026 16:59

This branch was previously deployed

1 inactive deployment
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.

1 participant