Skip to main content

API reference

Everything below is exported from map-gl-offline, and types ship with the package. OfflineMapManager is also the default export.

OfflineMapManager​

import { OfflineMapManager } from 'map-gl-offline';

const offlineManager = new OfflineMapManager();

Regions​

MethodReturnsDescription
downloadRegion(region, options?)Promise<DownloadRegionResult>Download everything a region needs. Guide
loadRegion(region, options?)Promise<DownloadRegionResult>Another name for downloadRegion
addRegion(region)Promise<void>Save a region's details only. Downloads nothing.
deleteRegion(regionId, styleId?)Promise<void>Delete a region, keeping what other regions need
getStoredRegion(regionId)Promise<StoredRegion | null>Get one region
listStoredRegions()Promise<StoredRegion[]>List regions with their storage details
listRegions()Promise<OfflineRegionOptions[]>List regions as they were defined
getRegionSize(regionId, styleId?)Promise<number>A region's tile size in bytes

Styles​

MethodReturnsDescription
downloadStyle(url, options?)Promise<StyleDownloadResult>Download a style. options adds provider, accessToken and forceProvider.
downloadMapboxStyle(url, token?, options?)Promise<StyleDownloadResult>Download as a Mapbox style
downloadMapLibreStyle(url, options?)Promise<StyleDownloadResult>Download as a MapLibre style
downloadStyleWithAutoDetection(url, options?)Promise<StyleDownloadResult>Work out the provider from the URL
loadStyleById(styleId)Promise<StyleEntry | null>Get a stored style
listStyles()Promise<StyleEntry[]>List stored styles
getStyleStats(styleId)Promise<EnhancedStyleStats>Sizes and counts for a style
deleteStyle(styleId)Promise<void>Delete a style, all its regions, and everything stored for them
cleanupOldStyles(maxAgeDays?)Promise<number>Delete old styles, returning how many

Analytics​

MethodReturns
getComprehensiveStorageAnalytics()Promise<StorageAnalyticsReport>
getRegionAnalytics()Promise<RegionAnalytics>
getTileStats(), getFontStats(), getSpriteStats(), getGlyphStats(), getModelStats()Stats for one kind of data
getFontAnalytics(), getSpriteAnalytics(), getGlyphAnalytics()More detail on one kind of data

Cleanup and maintenance​

MethodReturnsDescription
cleanupExpiredRegions()Promise<number>Delete regions last updated over 30 days ago. Doesn't read expiry.
forceCleanupExpiredRegions()Promise<number>Delete regions past their expiry
performSmartCleanup(options?)Promise<CleanupResult>Delete by age, count and size limits
setupAutoCleanup(options?)Promise<string>Start scheduled cleanup, returning its id. options adds intervalHours.
startEnhancedAutoCleanup(intervalHours?, options?)Promise<string>Start scheduled smart cleanup
stopAutoCleanup(cleanupId?)Promise<void>Stop a schedule
stopAllAutoCleanup()Promise<void>Stop every schedule
cleanupOldFonts(maxAge?), cleanupOldSprites(maxAge?), cleanupOldGlyphs(maxAge?), cleanupOldModels(maxAge?)Number deletedDelete old resources of one kind
verifyAndRepairFonts(), verifyAndRepairSprites(), verifyAndRepairGlyphs(), verifyAndRepairModels(){ verified, repaired, removed }Check one kind and remove damaged entries
performCompleteMaintenance(options?)Promise<MaintenanceResults>Clean up, verify and report in one call

Import and export​

MethodReturnsDescription
exportRegionAsMBTiles(regionId, options?)Promise<ExportResult>Export to MBTiles. Guide
importRegion(importData)Promise<ImportResult>Import an MBTiles file
downloadExportedRegion(exportResult)voidSave an export as a file in the browser

Individual resources​

Most apps should use downloadRegion. These download one kind of resource: downloadTilesWithOptions, downloadFontsWithOptions, downloadSpritesWithOptions, downloadGlyphsWithOptions, downloadModelsWithOptions. loadGlyphsForStyle reads stored glyphs.

OfflineManagerControl​

new OfflineManagerControl(offlineManager, options);

See The offline control for its options.

MethodDescription
updateStyleUrl(url)Change the style that new downloads use
getCurrentStyleUrl()Get that style
loadOfflineStyle(styleId)Show a stored style on the map
loadOfflineStyles()Show the stored style, asking which one if there are several
onAdd(map), onRemove()Called by the map when you add or remove the control

Types​

OfflineRegionOptions​

PropertyTypeDescription
idstringRequired. Unique within a style. Reusing one replaces that region.
namestringRequired. Display name.
boundsBoundingBoxRequired. [[west, south], [east, north]]
minZoom, maxZoomnumberRequired. Zoom range to download.
styleUrlstringThe style. Required by downloadRegion.
styleIdstringUse a style that's already stored
expirynumberAbsolute time in milliseconds since the epoch. Defaults to 30 days from now.
extraSourcesExtraSource[]More tile sources to save. Guide
tileExtensionstringForce one tile extension for every source. Rarely needed.
multipleRegions, deleteOnExpirybooleanDeprecated, with no effect. Regions already coexist, keyed by id. To delete expired regions, use forceCleanupExpiredRegions.

DownloadRegionOptions​

PropertyTypeDescription
onProgress(p: DownloadRegionProgress) => voidProgress updates
accessTokenstring | nullMapbox token, needed for mapbox:// URLs
provider'mapbox' | 'maplibre' | 'auto'Defaults to 'auto'
styleConfigRecord<string, unknown>Style config values
skipSprites, skipGlyphs, skipModelsbooleanLeave out a phase. Default false.
glyphRangesstring[]Font ranges to download, as 256-wide blocks. Defaults to common Unicode blocks.
tileOptionsTileDownloadOptionsSee below
signalAbortSignalCancels the download when aborted

DownloadRegionResult​

PropertyDescription
regionId, styleIdThe ids used
tileResulttotalTiles, downloadedTiles, skippedTiles, failedTiles, totalSize, errors and more
styleResult, spriteResults, glyphResult, modelResultResults of each phase, when it ran
runtimeAssetsCachedMapbox GL decoder files in the offline cache. Undefined for other styles.

TileDownloadOptions​

PropertyDefaultDescription
batchSize20 in downloadRegionTiles fetched at once
maxRetries3Retries for each tile
retryDelay1000Milliseconds before retrying
timeout10000Milliseconds for each tile
skipExistingfalse in downloadRegionLeave tiles that are already stored
storageQuotaChecktrueRefuse to start under 500 MB free
probeSourcesBeforeDownloadtrueSkip sources whose sample tiles are mostly missing
skipKnownSparseSourcestrueSkip Mapbox's sparse tilesets unless a switched-on feature needs them
skipInactiveSourcestrueSkip sources only hidden features use
priorityZoomLevelsZoom levels to fetch first
bandwidthLimitSlow downloads down
validateTilesfalseReject empty or invalid tiles
compressTilesfalseCompress image tiles before storing
onProgressProgress for this phase only

Other types​

TypeShape
BoundingBox[[west, south], [east, north]]
ExtraSource{ id, tiles, type?, minzoom?, maxzoom?, attribution? }. type is vector, raster or raster-dem.
RegionCleanupOptions{ maxAge?, maxStorageSize?, maxRegions?, priorityPatterns?, onProgress? }. Age in days, size in bytes.
CleanupResult{ scannedRegions, expiredRegions, deletedRegions, preservedRegions, freedSpace, errors, recommendations }
StorageAnalyticsReport{ tiles, fonts, sprites, glyphs, models, regions, totalStorageSize, storageByType, recommendations }
MaintenanceOptions{ cleanupExpired?, verifyIntegrity?, optimizeStorage?, generateReport?, onProgress? }
ExportResult{ success, format, filename, blob, size, statistics }
ImportResult{ success, regionId, message, warnings?, statistics }

Functions​

FunctionDescription
idbFetchHandler(url)Answer an idb:// URL from IndexedDB, as a Response
registerOfflineServiceWorker(url?)Register the Service Worker. Defaults to /idb-offline-sw.js.
unregisterOfflineServiceWorker()Remove it
convertStyleForServiceWorker(style)Rewrite a stored style's URLs for the Service Worker
configureProxy({ tiles?, fonts? })Send requests for certain hosts through your proxy
configureSqlJs({ wasmUrl?, wasmBinary? })Set where sql.js loads from
resetOfflineMapDB()Delete the database and everything in it
detectStyleProvider(url, style?)Returns 'mapbox' or 'maplibre'
resolveMapboxUrl(url, token)Turn a mapbox:// URL into an HTTPS one
i18n, t(key, options?)Language and translated text

dbPromise resolves to the open database, and OfflineMapDBVersionError is the error it throws when the version is incompatible. The services behind the manager, such as tileService, are also exported for advanced use.

Command line and Vite​

Description
npx map-gl-offline initCopy the Service Worker script to public/
offlineSwPlugin() from map-gl-offline/vite-pluginDo the same on every Vite build