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
|
- 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.
(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.
(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.
clear() → {Promise.<void>}
Empty the cache.
Returns:
- Type
- Promise.<void>
destroy()
Tear down. Removes the message listener; does NOT unregister the SW
(other tabs may still be using it).
disable()
enable()
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.
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.
Returns:
- Type
- Object
isEnabled()
isSupported() → {Boolean}
Detect whether the current environment supports the Service Worker.
Returns:
- Type
- Boolean
requestStats() → {Promise.<Object>}
Request a fresh snapshot from the SW (one-shot). Resolves with the stats.
Returns:
- Type
- Promise.<Object>