Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions modules/main/src/mapbox-legacy/mapbox/mapbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,16 @@ export default class Mapbox {
// intoto the new container from the props.
// Step 1: reparenting child nodes from old container to new container
const oldContainer = map.getContainer();
container.className = oldContainer.className;
while (oldContainer.childNodes.length > 0) {
container.appendChild(oldContainer.childNodes[0]);
// The old and new containers are the same node when effects re-run without
// the DOM being recreated (React StrictMode replay, Activity/bfcache
// restore) - moving childNodes[0] to the end of the same node would then
// loop forever, and the children are already in place.
if (oldContainer !== container) {
container.className = oldContainer.className;
oldContainer.querySelector('[mapboxgl-children]')?.remove();
while (oldContainer.childNodes.length > 0) {
container.appendChild(oldContainer.childNodes[0]);
}
}
// Step 2: replace the internal container with new container from the react component
// @ts-ignore
Expand Down Expand Up @@ -365,11 +372,6 @@ export default class Mapbox {
/* eslint-enable complexity,max-statements */

recycle() {
// Clean up unnecessary elements before storing for reuse.
const container = this.map.getContainer();
const children = container.querySelector('[mapboxgl-children]');
children?.remove();

Mapbox.savedMaps.push(this);
}

Expand Down
18 changes: 10 additions & 8 deletions modules/react-mapbox/src/mapbox/mapbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,16 @@ export default class Mapbox {
// intoto the new container from the props.
// Step 1: reparenting child nodes from old container to new container
const oldContainer = map.getContainer();
container.className = oldContainer.className;
while (oldContainer.childNodes.length > 0) {
container.appendChild(oldContainer.childNodes[0]);
// The old and new containers are the same node when effects re-run without
// the DOM being recreated (React StrictMode replay, Activity/bfcache
// restore) - moving childNodes[0] to the end of the same node would then
// loop forever, and the children are already in place.
if (oldContainer !== container) {
container.className = oldContainer.className;
oldContainer.querySelector('[mapboxgl-children]')?.remove();
while (oldContainer.childNodes.length > 0) {
container.appendChild(oldContainer.childNodes[0]);
}
}
// Step 2: replace the internal container with new container from the react component
// @ts-ignore
Expand Down Expand Up @@ -382,11 +389,6 @@ export default class Mapbox {
/* eslint-enable complexity,max-statements */

recycle() {
// Clean up unnecessary elements before storing for reuse.
const container = this.map.getContainer();
const children = container.querySelector('[mapboxgl-children]');
children?.remove();

Mapbox.savedMaps.push(this);
}

Expand Down
18 changes: 10 additions & 8 deletions modules/react-maplibre/src/maplibre/maplibre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,16 @@ export default class Maplibre {
// intoto the new container from the props.
// Step 1: reparenting child nodes from old container to new container
const oldContainer = map.getContainer();
container.className = oldContainer.className;
while (oldContainer.childNodes.length > 0) {
container.appendChild(oldContainer.childNodes[0]);
// The old and new containers are the same node when effects re-run without
// the DOM being recreated (React StrictMode replay, Activity/bfcache
// restore) - moving childNodes[0] to the end of the same node would then
// loop forever, and the children are already in place.
if (oldContainer !== container) {
container.className = oldContainer.className;
oldContainer.querySelector('[mapboxgl-children]')?.remove();
while (oldContainer.childNodes.length > 0) {
container.appendChild(oldContainer.childNodes[0]);
}
}
// Step 2: replace the internal container with new container from the react component
// @ts-ignore
Expand Down Expand Up @@ -361,11 +368,6 @@ export default class Maplibre {
/* eslint-enable complexity,max-statements */

recycle() {
// Clean up unnecessary elements before storing for reuse.
const container = this.map.getContainer();
const children = container.querySelector('[mapboxgl-children]');
children?.remove();

Maplibre.savedMaps.push(this);
}

Expand Down