Skip to content

fix(web): load in Expo Snack without AssetRegistry and with module-worker mapbox-gl - #4411

Merged
mfazekas merged 1 commit into
mainfrom
fix/web-snack-asset-registry-worker
Oct 27, 2026
Merged

mfazekas merged 1 commit into
mainfrom
fix/web-snack-asset-registry-worker

Conversation

@mfazekas

Copy link
Copy Markdown
Contributor

Description

Follow-up to #4408. Testing 10.3.12 in a real Expo Snack (web, SDK 54) turned up two more problems:

  1. Importing @rnmapbox/maps throws Cannot read properties of undefined (reading 'registerAsset'). Snack's web bundler maps @react-native/assets-registry/registry to require('react-native').AssetRegistry, which react-native-web doesn't have. The registry is now required lazily inside resolveAsset, with a guard. Metro and Expo web behave as before. In Snack, numeric asset ids resolve to undefined.
  2. With mapbox-gl 3.33 (what Snack resolves), the documented setWorkerUrl snippet fails with Module scripts don't support importScripts() because mapbox-gl starts workerUrl as a module worker. setWorkerUrl now also sets mapboxgl.workerClass to start a classic Worker(url). mapbox-gl 3.15.0 and 3.33.0 both use workerClass in place of workerUrl when it is set. Nothing changes if setWorkerUrl isn't called.

The Snack snippet in src/web/install.md stays the same.

Verification: on snack.expo.dev, published 10.3.12 with no workarounds fails with (1). With only an AssetRegistry patch in the app, it fails with (2). Snack can only install from npm, so to test this branch I built lib/ with a webpack config that copies Snackager's (javascript/auto, babel-loader with @react-native/babel-preset). In that build the registry maps to the same require('react-native').AssetRegistry shim. I pasted the result into a Snack App.js that uses only the documented setWorkerUrl snippet. On the real Snack web runtime, the 10.3.12 build still throws (1). This branch renders a streets map with no worker errors, with both mapbox-gl 3.33.0 and 3.15.0 (map.loaded() true, 457 rendered features, tiles 200).

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.
    • 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

Web only. Before: Snack shows the registerAsset error. After: the Snack renders the map.

Component to reproduce the issue you're fixing

Snack (web), dependencies @rnmapbox/[email protected],mapbox-gl:

import React, { useEffect, useState } from 'react';
import { Platform, Text, View } from 'react-native';
import Mapbox 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() {
  const [ready, setReady] = useState(false);
  useEffect(() => {
    // Set a token here; the test fetched a restricted one at runtime.
    Mapbox.setAccessToken('<token>');
    setReady(true);
  }, []);
  if (!ready) return <Text>Loading…</Text>;
  return (
    <View style={{ flex: 1 }}>
      <Mapbox.MapView style={{ flex: 1 }}>
        <Mapbox.Camera zoomLevel={2} centerCoordinate={[0, 20]} />
      </Mapbox.MapView>
    </View>
  );
}

…-worker mapbox-gl

Require the asset registry lazily, since Snack's web bundle maps it to
react-native's AssetRegistry, which react-native-web lacks, and importing
@rnmapbox/maps threw. setWorkerUrl now also sets mapboxgl.workerClass to start
a classic worker, so the CSP worker's importScripts() works with mapbox-gl
versions that start workerUrl as a module worker.
@mfazekas
mfazekas merged commit ea7ca89 into main Oct 27, 2026
10 checks passed
@mfazekas
mfazekas deleted the fix/web-snack-asset-registry-worker branch October 27, 2026 01:30

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