nextjs - 💡(How to fix) Fix Docs: Preserving UI State guide missing warning for imperative DOM/WebGL libraries (Mapbox GL, Three.js, etc.) [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#93495Fetched 2026-05-06 06:11:48
View on GitHub
Comments
2
Participants
2
Timeline
4
Reactions
0
Assignees
Timeline (top)
commented ×2assigned ×1issue_type_added ×1

Error Message

TypeError: Cannot read properties of undefined (reading 'appendChild') at Marker.addTo at recursivelyTraverseReappearLayoutEffects

Code Example

TypeError: Cannot read properties of undefined (reading 'appendChild')
    at Marker.addTo
    at recursivelyTraverseReappearLayoutEffects
RAW_BUFFERClick to expand / collapse

What is the documentation issue?

The cacheComponents docs mention that effects are cleaned up on hide and recreated on reappear, and links to the Preserving UI State guide for common patterns. However, the guide's "Effect and media cleanup" section only covers timers and media elements <video>, <audio>, it's missing a critical third category

Libraries like Mapbox GL, Three.js, Leaflet, and Google Maps destroy their internal DOM references during useEffect cleanup (e.g., map.remove() sets _container = null). When Activity reappears the route, effects re-run but the library instance is in a "destroyed" state, leading to a crash:

TypeError: Cannot read properties of undefined (reading 'appendChild')
    at Marker.addTo
    at recursivelyTraverseReappearLayoutEffects

Critical detail: This only crashes in production builds (Webpack). Dev mode (Turbopack) never triggers it, making it extremely difficult to diagnose.

Proposed Fix: Remount the library from scratch on reappear instead of reusing the stale, destroyed instance.

A warning and example for this pattern in the Preserving UI State guide would prevent significant debugging time for anyone using mapping, 3D, or canvas-based libraries.

Is there any context that might help us understand?

Context:

  • Confirmed with Next.js 16 + react-map-gl v8 / Mapbox GL JS v3.
  • Related to issue #86577.

I'd be happy to contribute a docs PR adding this pattern to the Preserving UI State guide if the team confirms the recommended approach. Let me know if this would be welcomed.

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/app/guides/preserving-ui-state

extent analysis

TL;DR

Remounting the library from scratch on reappear instead of reusing the stale, destroyed instance may fix the crash in production builds.

Guidance

  • Verify that the issue only occurs in production builds with Webpack and not in development mode with Turbopack.
  • Check the documentation for the library being used (e.g., Mapbox GL, Three.js, Leaflet, Google Maps) to see if it provides any guidance on handling the destruction and remounting of instances.
  • Consider adding a warning and example to the Preserving UI State guide to prevent similar issues for other users.
  • Test the proposed fix by remounting the library instance on reappear and verify that it resolves the crash.

Example

No code snippet is provided as the issue does not include specific code that needs to be modified.

Notes

The issue seems to be specific to production builds with Webpack and may not be reproducible in development mode with Turbopack. The proposed fix is to remount the library instance on reappear, but this may need to be adapted to the specific library being used.

Recommendation

Apply the proposed workaround of remounting the library instance on reappear, as it is a targeted solution to the specific issue being experienced.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - 💡(How to fix) Fix Docs: Preserving UI State guide missing warning for imperative DOM/WebGL libraries (Mapbox GL, Three.js, etc.) [2 comments, 2 participants]