[Bug] Marker crashes with "appendChild" error during rapid client-side navigation (React 19 / Next.js 16)

Đang mở
#2,584 1 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

  • #2585 của @officialasishkumar — đã đóng, không merge

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
nextjs, react, typescript

Hướng nghiên cứu

Bắt đầu với modules/react-mapbox/src/components/marker.ts, đặc biệt là useEffect xung quanh dòng được liên kết, và tái hiện thao tác điều hướng nhanh phía client được mô tả trong issue. Kiểm tra cách xử lý được đề xuất dựa trên vòng đời của Marker và hành vi cleanup. Được xem là hoàn tất khi thao tác điều hướng nhanh không còn gây ra lỗi crash appendChild mà không làm hỏng việc mount và gỡ bỏ Marker thông thường.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug
Description

The Marker component crashes with Cannot read properties of undefined (reading 'appendChild') during rapid client-side navigation (e.g., quickly navigating between a random page and a page that contains a <Map> with <Marker> children).

Expected Behavior

No response

Steps to Reproduce

TL:DR steps to reproduce:

Quickly navigate back and forth between pages which contain a <Map> with <Marker> children.

Steps to Reproduce

  1. Create a Next.js 16 app with React 19
  2. Page A: a list page (no map)
  3. Page B: a detail page with containing children
  4. Navigate rapidly between Page A and Page B using client-side navigation (clicking links, not full page loads)
  5. After a few rapid back-and-forth navigations, the error appears

Root Cause

The Marker component's useEffect at marker.ts#L86 calls marker.addTo(map.getMap()) with [] deps. During rapid unmount/remount cycles in React 19, the new mount's useEffect can fire before the previous mount's cleanup has run. At that point, mapbox-gl's Map instance still exists but its internal DOM container sub-elements have been torn down, causing marker.addTo() to fail when it tries to appendChild into a destroyed container.

Through instrumentation, I confirmed:

  • map.getMap() returns a valid Map instance (truthy)
  • map._container exists (truthy)
  • But inside marker.addTo(), deeper container child elements are undefined
  • The error is intermittent and depends on navigation speed

Suggested Fix

Wrap marker.addTo(map.getMap()) in a try-catch in the useEffect

useEffect(() => {
    try {
        marker.addTo(map.getMap());
    } catch {
        // Map container was destroyed during rapid navigation — safe to ignore
        return;
    }
    return () => {
        marker.remove();
    };
}, []);
Environment
  • react-map-gl: 8.1.0 (via @vis.gl/react-mapbox)
  • mapbox-gl: 3.11.0
  • React: 19.1.0
  • Next.js: 16.2.2 (App Router, Turbopack)
  • Browser: Chrome
Logs

No response

Ngôn ngữ chính
TypeScript
Star
8.5k
Fork
1.3k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của visgl/react-map-gl

Tất cả issue của visgl/react-map-gl

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.