Class: BrowserCache

bemap.BrowserCache(optionsopt)

new BrowserCache(optionsopt)

BeNomad BeMap JavaScript API — BrowserCache (v2.0) Page-side companion to `dist/bemap-sw-tiles.js`. Wraps the Service Worker registration, the INIT handshake, and the enable/disable/stats message channel. The instance is owned by `bemap.MapLibreMap` and exposed through these map methods: - `map.enableBrowserCache()` → `cache.enable()` - `map.disableBrowserCache()` → `cache.disable()` - `map.getBrowserCacheStats()` → `cache.getStats()` - `map.clearBrowserCache()` → `cache.clear()` Persists the on/off toggle in `localStorage` under the key `bemap_browser_cache` so customer apps can wire it to a consent banner.
Parameters:
Name Type Attributes Description
options Object <optional>
Properties
Name Type Attributes Default Description
tilesHost String <optional>
Forwarded to the SW so it knows which host to intercept.
serviceWorkerPath String <optional>
'/bemap-sw-tiles.js'
onStats function <optional>
Live `{ hits, misses }` updates.
onError function <optional>
Called with a `bemap.Error` on registration / message failures.
Since:
  • 2.0.0
Source:

Methods

(static) _findBemapScriptDir()

Locate the directory the `bemap-js-api.js` (or `.min.js`) bundle was loaded from, so we can derive sensible default SW paths when the consumer hasn't pinned one. Returns a path ending in `/`, or `null` if not detectable.
Source:

(static) _findBemapScriptInfo()

Locate the bemap-js-api bundle's script tag. Returns `{ src, dir }` where `src` is the absolute URL we matched and `dir` is its directory (ending in `/`). Used both for SW path discovery and for diagnostic warnings.
Source:

(static) unregisterStale()

Unregister any previously-installed bemap tile-cache Service Worker on this origin and delete its Cache Storage. Called automatically when browserCache is OFF (the default) so a stale / mis-scoped worker can't keep controlling the page and serving cached range data (which can corrupt PMTiles rendering and stop the map loading). Safe no-op when none exist.
Since:
  • 2.0.0
Source:

_candidatePaths()

Build the ordered list of paths to try when registering the SW. When the caller explicitly passed `opts.serviceWorkerPath` we honour that exactly (single entry, no fallback). Otherwise we try, in order: 1. `/bemap-sw-tiles.js` — canonical site-root install 2. `../bemap-sw-tiles.js` — common: SW lives one level up from the dist/ folder the bundle ships in (e.g. evmove root) 3. `bemap-sw-tiles.js` — SW sits next to the bundle inside dist/. Scope is narrower but still works for apps whose pages live under that directory.
Source:

clear() → {Promise.<void>}

Empty the cache.
Source:
Returns:
Type
Promise.<void>

destroy()

Tear down. Removes the message listener; does NOT unregister the SW (other tabs may still be using it).
Source:

disable()

Source:

enable()

Source:

getRegistrationAttempts() → {Array.<String>}

The full list of registration attempts from the last `register()` call — each entry is `" → "`. Exposed so the wrapper can surface a useful warning when nothing worked.
Source:
Returns:
Type
Array.<String>

getStats() → {Object}

Returns the latest known stats. The SW broadcasts updates regularly via `'cache:stats'` (see the map's `on()`); this method returns the locally cached snapshot synchronously.
Source:
Returns:
Type
Object

isEnabled()

Source:

isSupported() → {Boolean}

Detect whether the current environment supports the Service Worker.
Source:
Returns:
Type
Boolean

requestStats() → {Promise.<Object>}

Request a fresh snapshot from the SW (one-shot). Resolves with the stats.
Source:
Returns:
Type
Promise.<Object>