Repository navigation
fix(web): load in Expo Snack without AssetRegistry and with module-worker mapbox-gl - #4411
Merged
Merged
Conversation
…-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.
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Follow-up to #4408. Testing 10.3.12 in a real Expo Snack (web, SDK 54) turned up two more problems:
@rnmapbox/mapsthrowsCannot read properties of undefined (reading 'registerAsset'). Snack's web bundler maps@react-native/assets-registry/registrytorequire('react-native').AssetRegistry, which react-native-web doesn't have. The registry is now required lazily insideresolveAsset, with a guard. Metro and Expo web behave as before. In Snack, numeric asset ids resolve toundefined.setWorkerUrlsnippet fails withModule scripts don't support importScripts()because mapbox-gl startsworkerUrlas a module worker.setWorkerUrlnow also setsmapboxgl.workerClassto start a classicWorker(url). mapbox-gl 3.15.0 and 3.33.0 both useworkerClassin place ofworkerUrlwhen it is set. Nothing changes ifsetWorkerUrlisn't called.The Snack snippet in
src/web/install.mdstays 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 samerequire('react-native').AssetRegistryshim. I pasted the result into a SnackApp.jsthat uses only the documentedsetWorkerUrlsnippet. 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
CONTRIBUTING.mdyarn generatein the root folder/exampleapp./example)Screenshot OR Video
Web only. Before: Snack shows the
registerAsseterror. After: the Snack renders the map.Component to reproduce the issue you're fixing
Snack (web), dependencies
@rnmapbox/[email protected],mapbox-gl: