Skip to content

Handle missing map style when finding fonts - #2125

Merged
tananaev merged 3 commits into
traccar:masterfrom
bernamaxim:fix-fonts-style-loading
Nov 27, 2026
Merged

tananaev merged 3 commits into
traccar:masterfrom
bernamaxim:fix-fonts-style-loading

Conversation

@bernamaxim

Copy link
Copy Markdown
Contributor

findFonts destructures map.getStyle(), but MapLibre returns undefined
from getStyle() while a style is still loading. If a map layer component
re-renders during that window (for example when new data arrives right after
the style is reloaded), the UI crashes with:

Cannot destructure property 'glyphs' of 'e.getStyle(...)' as it is undefined

I see this regularly on mobile after switching back to the Traccar tab.

This change falls back to the default fonts when the style is not available
yet. Layers are re-created once the map is ready, so the correct fonts are
applied after the style finishes loading.

@tananaev

Copy link
Copy Markdown
Member

This prevents the exception, but could you share a full stack trace and reproduction steps? MapView already marks the map as not ready before setStyle() and hides its children until loading finishes, so it would help to understand why findFonts() runs during that window.

I couldn’t reproduce the application crash with delayed style responses and page freeze/resume. Calling findFonts() directly during loading reproduces the exception, but adding a source then also throws “Style is not done loading.” If layers are running before the style is ready, we may need to fix the readiness lifecycle rather than only fall back to default fonts.

@bernamaxim

Copy link
Copy Markdown
Contributor Author

I was able to reproduce it reliably. The trigger is a WebGL context loss,
which mobile browsers do to background tabs - that's why it only showed up
after switching tabs.

In MapLibre 6, _contextLost destroys the style and sets map.style = null
until the context is restored (it then rebuilds it from a snapshot). During
that time getStyle() returns undefined, but MapView still reports the map
as ready, so any re-render of MapGeofence (e.g. on a position update) calls
findFonts() and crashes.

Tested on Traccar 6.17.0 (app and server). The relevant code is unchanged on
master.

Steps:

  1. Open the main map with at least one geofence visible.
  2. In the console:
    const ext = document.querySelector('canvas.maplibregl-canvas')
      .getContext('webgl2').getExtension('WEBGL_lose_context');
    ext.loseContext();
  3. Wait for a position update.

Stack trace (minified build, Uq is findFonts and s is MapGeofence):

TypeError: Cannot destructure property 'glyphs' of 'e.getStyle(...)' as it is undefined.
    at Uq (https://example.com/assets/index-fTz37379.js:852:251873)
    at s (https://example.com/assets/MapGeofence-C41B_tXw.js:1:766)
    at as (https://example.com/assets/index-fTz37379.js:9:49242)
    at Qc (https://example.com/assets/index-fTz37379.js:9:72060)
    at vl (https://example.com/assets/index-fTz37379.js:9:82980)
    at cf (https://example.com/assets/index-fTz37379.js:9:128370)
    at af (https://example.com/assets/index-fTz37379.js:9:127427)
    at rf (https://example.com/assets/index-fTz37379.js:9:127261)
    at Gd (https://example.com/assets/index-fTz37379.js:9:123970)
    at Vf (https://example.com/assets/index-fTz37379.js:9:136502)

With this change the same steps no longer crash, and after
ext.restoreContext() all layers come back from MapLibre's snapshot with the
correct fonts, no duplicates and no "Style is not done loading" errors.

@tananaev

Copy link
Copy Markdown
Member

Thanks, that explains why the earlier test didn’t reproduce it. If WebGL context loss is expected behavior, shouldn’t we make sure the map recovers properly? Does this change also handle data or layer changes that happen while the context is lost?

@tananaev

Copy link
Copy Markdown
Member

Thanks, I’ve now reproduced the crash and confirmed your original fix resolves it. Sorry for the confusion—the original change is sufficient for this issue.

@bernamaxim

Copy link
Copy Markdown
Contributor Author

Thanks for checking! I've reverted the MapView change, so the PR is back to
the original one-line fix.

@tananaev
tananaev merged commit d9b7134 into traccar:master Nov 27, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants