Skip to content

fix(mapbox): guard getProjection against throw when map style is not ready - #10551

Open
waterWang wants to merge 1 commit into
visgl:masterfrom
waterWang:fix/getProjection-throw-safety
Open

fix(mapbox): guard getProjection against throw when map style is not ready#10551
waterWang wants to merge 1 commit into
visgl:masterfrom
waterWang:fix/getProjection-throw-safety

Conversation

@waterWang

Copy link
Copy Markdown

Description

getProjection() in @deck.gl/mapbox's deck-utils.ts can throw when the underlying map's style is not yet assigned, instead of returning undefined the way callers (getDefaultView / MapboxOverlay#_onAddInterleaved) expect for "not ready yet, default to mercator".

The existing optional-chaining (map.getProjection?.()) only guards against the method being absent, not against the call itself throwing. In maplibre-gl, Map.prototype.getProjection() is return this.style.getProjection() with no guard on this.style, so calling it before the style exists throws a TypeError.

This is reproducible under React 19 StrictMode: the dev-only double-invoke of mount effects adds, removes, and re-adds the same overlay, and the third addControl call hits a still-styleless map.

Fix

Wrap the map.getProjection?.() call in a try/catch and return 'mercator' as the safe default. This matches the callers' existing expectation — getDefaultView already treats any non-'globe' return as mercator.

Related

…ready

map.getProjection() can throw (e.g. maplibre-gl's implementation calls
this.style.getProjection() without a guard on this.style) when invoked
before the map's style is attached — such as on the third addControl
call under React 19 StrictMode's dev-only double-invoke of mount effects.

The existing optional-chaining only guards against the method being
absent, not against the call itself throwing. Wrap the call in try/catch
and return 'mercator' as the safe default, matching what callers
(getDefaultView / MapboxOverlay#_onAddInterleaved) already expect for the
not-ready-yet case.

Fixes visgl#10549
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.

getProjection() in @deck.gl/mapbox can throw instead of returning undefined when called before the map's style is assigned

1 participant