Fix reuseMaps infinite loop when reusing into the same container - #2616
Open
mikeyfarina wants to merge 1 commit into
Open
mikeyfarina wants to merge 1 commit into
mikeyfarina wants to merge 1 commit into
Conversation
Contributor
|
Do we have or should we have some kind of test file or maybe demo page that shows the error in the different cases before (and fixed after)? |
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.
Fixes #2613.
With
reuseMaps,Mapbox.reuse()moves the pooled map's children from its old container into the mount container withwhile (oldContainer.childNodes.length > 0) container.appendChild(oldContainer.childNodes[0]). When the two containers are the same DOM node, the loop never terminates and the tab freezes at 100% CPU. That case is reachable whenever the<Map>mount effect re-runs while the DOM survives — React StrictMode's effect replay in development, and back/forward navigation restore (Activity semantics, e.g. Next.js 16 App Router) in production builds.Changes, applied to all three copies of the pattern (
react-mapbox,react-maplibre,mapbox-legacy):reuse()treatsoldContainer === containeras "already in place" and skips the reparenting.[mapboxgl-children]cleanup moves fromrecycle()intoreuse()'s different-container branch. Removing it at recycle time broke the same-container case a second way:reuse()returns the same instance, sosetMapInstancebails out onObject.isand React never re-inserts the children div it still owns — any plain-JSX map child silently disappeared after the reuse. Removing it only when the DOM is actually moving keeps the surviving React tree intact, and the different-container behavior is unchanged (same removal, later moment).yarn test: 52 files / 84 tests pass. Thereact-mapboxversion of this change has also been running in production as a vendored patch since 2026-08-27, including a browser regression test that walks map pin → popup → team page → browser back (the sequence that previously froze on the first back-navigation).https://claude.ai/code/session_01DmN5k333nk7YLAhtyHE8em