Add error handling for Google Maps DOM conflicts with React
The Google Maps API and React both manipulate the DOM, which can cause conflicts where Google Maps removes nodes that React still has references to. Add graceful error handling: 1. Remove async flag from Google Maps script - use defer only 2. Add try-catch in marker update useEffect to ignore removeChild errors 3. Add cleanup function to properly tear down markers on unmount 4. Log warnings instead of crashing when DOM conflicts occur This allows the app to continue functioning even when there are minor DOM reconciliation issues between Google Maps and React. 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -82,12 +82,26 @@ export const StationMap: React.FC<StationMapProps> = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
initMap();
|
initMap();
|
||||||
|
|
||||||
|
// Cleanup: clear markers when component unmounts
|
||||||
|
return () => {
|
||||||
|
try {
|
||||||
|
markers.current.forEach((marker) => marker.setMap(null));
|
||||||
|
infoWindows.current.forEach((iw) => iw.close());
|
||||||
|
markers.current = [];
|
||||||
|
infoWindows.current = [];
|
||||||
|
} catch (err) {
|
||||||
|
// Silently ignore cleanup errors - they don't affect the user
|
||||||
|
console.debug('[StationMap] Cleanup error (ignored):', err);
|
||||||
|
}
|
||||||
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Update markers when stations or saved status changes
|
// Update markers when stations or saved status changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!map.current) return;
|
if (!map.current) return;
|
||||||
|
|
||||||
|
try {
|
||||||
// Clear old markers and info windows
|
// Clear old markers and info windows
|
||||||
markers.current.forEach((marker) => marker.setMap(null));
|
markers.current.forEach((marker) => marker.setMap(null));
|
||||||
infoWindows.current.forEach((iw) => iw.close());
|
infoWindows.current.forEach((iw) => iw.close());
|
||||||
@@ -136,6 +150,16 @@ export const StationMap: React.FC<StationMapProps> = ({
|
|||||||
if (allMarkers.length > 0) {
|
if (allMarkers.length > 0) {
|
||||||
fitBoundsToMarkers(map.current, allMarkers);
|
fitBoundsToMarkers(map.current, allMarkers);
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Ignore DOM errors from Google Maps conflicting with React reconciliation
|
||||||
|
// This is a known issue when Google Maps removes DOM nodes that React still has references to
|
||||||
|
if (error instanceof Error && error.message.includes('removeChild')) {
|
||||||
|
console.warn('[StationMap] Ignoring Google Maps DOM conflict:', error.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Re-throw other errors
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}, [stations, savedPlaceIds, currentLocation, onMarkerClick]);
|
}, [stations, savedPlaceIds, currentLocation, onMarkerClick]);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
|||||||
@@ -51,8 +51,7 @@ export function loadGoogleMaps(): Promise<void> {
|
|||||||
|
|
||||||
// Create script tag with callback
|
// Create script tag with callback
|
||||||
const script = document.createElement('script');
|
const script = document.createElement('script');
|
||||||
script.src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&loading=async&callback=${callbackName}&libraries=places`;
|
script.src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&callback=${callbackName}&libraries=places`;
|
||||||
script.async = true;
|
|
||||||
script.defer = true;
|
script.defer = true;
|
||||||
|
|
||||||
script.onerror = () => {
|
script.onerror = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user