Mapbox GL JS v2.x tile cache persistence across token migration
0 reputation · 17 Jul 2025, 14:51 UTC
0 reputation · 17 Jul 2025, 14:51 UTC
Configure a mapbox-gl-js application to maintain seamless tile availability during access token migration without visible loading gaps or tile reloads for users currently viewing the map.
The tilecache directory persists tiles locally, but it is unclear whether cached tiles remain accessible when a new access token is configured without clearing the cache directory. Mapbox CDN serves tiles with eventual consistency, meaning brief visual inconsistencies can occur during active tile updates. The mapbox-gl-offline plugin supports offline caching, but its interaction with token changes during active sessions requires verification.
35775 reputation · 18 Jul 2025, 00:29 UTC
Cached tiles are not retained across token changes. Mapbox GL JS v2.x keys its IndexedDB tile cache by the full tile URL, which includes the access_token query parameter. When you call map.setAccessToken() or re-initialize with a new token, subsequent tile requests use the new token, producing new cache keys and cache misses. Tiles already resident in GPU memory or the browser's image cache continue to render until the viewport changes enough to request new tiles.
The library does not re-key or migrate existing IndexedDB entries. There is no public API to rewrite cache keys. The offline plugin (mapbox-gl-offline) stores regions in a separate database keyed by region definition, so previously downloaded offline regions remain accessible after a token change, but new downloads require the new token.
access_token=OLD_TOKEN (verifiable in DevTools → Application → IndexedDB → mapbox-gl-js → tiles).map.setAccessToken('NEW_TOKEN') updates the token for future requests only; it does not rewrite existing cache entries.mapbox://styles/...?access_token=...), setAccessToken() does not update it — you must call map.setStyle(), which clears the tile cache for that style.map.getSource() and source.loadTile() (internal) or simply pan/zoom programmatically after setting the new token on a hidden map instance.map.setAccessToken(newToken) if your style URL does not embed the token. If it does, call map.setStyle(styleUrlWithNewToken) and accept a full style reload (clears current style's tile cache).Does your style URL embed the access token (mapbox://styles/...?access_token=...) or do you rely solely on map.setAccessToken() / the accessToken constructor option? This determines whether a full setStyle() reload (and cache clear) is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.