Mapbox GL JS token rotation handling after deployment: cache inconsistency
0 reputation · 14 Jan 2023, 07:30 UTC
0 reputation · 14 Jan 2023, 07:30 UTC
When deploying a Mapbox GL JS application, developers often rotate access tokens to limit exposure or comply with security policies. The goal is to allow the map to continue loading tiles after a token change without forcing users to reload the entire page.
However, the interaction between token rotation and browser caching layers—such as the HTTP cache, service‑worker cache, and Mapbox GL JS internal tile cache—is not consistently defined across browsers, leading to situations where stale tokens are still used and requests fail with 401 errors until a hard refresh occurs.
What approaches can reliably invalidate cached map tiles when a token is rotated? How do major browsers treat stale tokens in their service‑worker versus HTTP caches? Does Mapbox GL JS provide a setting to force token refresh on each tile request?
33275 reputation · 14 Jan 2023, 09:20 UTC
Mapbox GL JS builds each tile URL by appending the current accessToken as a query parameter. Changing the token therefore changes the URL, so browsers treat the request as a new resource and bypass the HTTP cache for that request. However, tile responses are often served with a long Cache‑Control header (default 7 days). If a service worker is registered, it can cache those responses independently, and the old token can remain in the service‑worker cache until the worker is updated or the cache is purged. Mapbox GL JS itself offers no configuration to force a token refresh on each request; it simply relies on the browser’s caching layers.
max‑age.Cache‑Control: max‑age=604800 (7 days).mapboxgl.accessToken = NEW_TOKEN; before creating or reloading the map.map.setStyle(STYLE_URL);. This clears Mapbox GL JS’s internal tile cache and forces new tile requests.STYLE_URL + '?v=' + Date.now(). This guarantees a new URL that bypasses both HTTP and service‑worker caches for that request.self.addEventListener('message', event => {
if (event.data && event.data.type === 'clear-mapbox-cache') {
caches.delete('mapbox-tiles').then(() => console.log('Mapbox cache cleared'));
}
});
Then trigger it from the page after updating the token:
navigator.serviceWorker.controller.postMessage({type: 'clear-mapbox-cache'});
From cache column shows Network or Service Worker is not used. A 401 should no longer appear.If you use Workbox, you can set expiration: {maxAgeSeconds: 0} for Mapbox tiles during token rotation, forcing a network fetch on the next request.
Does your application register a service worker? If so, the service‑worker cache must be cleared after the token change; otherwise the old token may still be served until the cache expires.
Token rotation is safe as long as you (1) reload the style, (2) add a cache‑busting query string, and (3) clear any service‑worker tile cache. Mapbox GL JS itself cannot force a token refresh; the browser’s caching layers and your own cache‑busting strategy are the only reliable controls.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.