visgl / visgl/react-map-gl

[Bug] Error: Source "mapbox-dem" cannot be removed while terrain is using it.

Open
#2,553 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
8.5k
Forks
1.4k
Avg merge
5d 17h
Merged PRs (30d)
3

Description

Description

This is the code from your example:

  return (
    <>
      <Map
        initialViewState={{
          latitude: 32.6141,
          longitude: -114.34411,
          zoom: 14,
          bearing: 80,
          pitch: 80,
        }}
        maxPitch={85}
        mapStyle="mapbox://styles/mapbox/satellite-v9"
        mapboxAccessToken={env.NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN}
        terrain={{ source: "mapbox-dem", exaggeration: 1.5 }}
      >
        <Source
          id="mapbox-dem"
          type="raster-dem"
          url="mapbox://mapbox.mapbox-terrain-dem-v1"
          tileSize={512}
          maxzoom={14}
        />
      </Map>
    </>
  );

When you add reuseMaps to the Map component, and you navigate to another page (so you unmount Map)

You have this error:

Error: Source "mapbox-dem" cannot be removed while terrain is using it.

I can fix is creating a cleanup composant like :

function MapCleanup() {
  const { current: map } = useMap();

  React.useEffect(() => {
    return () => {
      if (map) {
        try {
          map.getMap().setTerrain(undefined);
        } catch (error) {
          console.warn("Error:", error);
        }
      }
    };
  }, [map]);

  return null;
}

And use it like:

  return (
    <>
      <Map
        reuseMaps
        initialViewState={{
          latitude: 32.6141,
          longitude: -114.34411,
          zoom: 14,
          bearing: 80,
          pitch: 80,
        }}
        maxPitch={85}
        mapStyle="mapbox://styles/mapbox/satellite-v9"
        mapboxAccessToken={env.NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN}
        terrain={{ source: "mapbox-dem", exaggeration: 1.5 }}
      >
        <MapCleanup />
        <Source
          id="mapbox-dem"
          type="raster-dem"
          url="mapbox://mapbox.mapbox-terrain-dem-v1"
          tileSize={512}
          maxzoom={14}
        />
      </Map>
    </>
  );

But its a lot of lines... Would it be possible to add map.getMap().setTerrain(undefined); when reuseMaps unmount mapbox-dem source ?

Expected Behavior

No response

Steps to Reproduce

See my message up

Environment
  • Framework version: "react-map-gl": "^8.0.4",
  • Map library: "mapbox-gl": "^3.14.0",
  • Browser: Chrome 139.0.7258.138
  • OS: Ubuntu
Logs

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how Map handles reuseMaps unmounting and how the nested Source is removed when terrain references it. Reproduce the example with reuseMaps, terrain, and the mapbox-dem Source, then compare the existing cleanup behavior with the reported MapCleanup workaround. Done means navigating away no longer raises the source-removal error.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.