For AI agents: a documentation index is available at /llms.txt. Markdown versions of all pages are available by appending .md to any URL.
Function addProtocol
Parameters
- customProtocol: string
the protocol to hook, for example 'custom'
- loadFn: AddProtocolAction
the function to use when trying to fetch a tile specified by the customProtocol
Returns void
Example
// This will fetch a file using the fetch API (this is obviously a non interesting example...) addProtocol('custom', async (params, abortController) => { const t = await fetch(`https://${params.url.split("://")[1]}`); if (t.status == 200) { const buffer = await t.arrayBuffer(); return {data: buffer} } else { throw new Error(`Tile fetch error: ${t.statusText}`); } }); // the following is an example of a way to return an error when trying to load a tile addProtocol('custom2', async (params, abortController) => { throw new Error('someErrorMessage'); }); // An image handler that draws or computes its tiles can return them decoded, as an ImageBitmap. addProtocol('drawn', async (params, abortController) => { const canvas = new OffscreenCanvas(256, 256); // ... draw the tile ... return {data: await createImageBitmap(canvas)}; });- customProtocol: string
Adds a custom load resource function that will be called when using a URL that starts with a custom url schema. This will happen in the main thread, and workers might call it if they don't know how to handle the protocol. The example below will be triggered for custom:// urls defined in the sources list in the style definitions. The function passed will receive the request parameters and should return with the resulting resource.
requestParameters.typesays which kind of resource is expected. A tile is anArrayBuffer, for example a non-compressed pbf vector tile. An image is either its encoded bytes or anImageBitmap/HTMLImageElement, which is used without decoding. See AddProtocolResponseData for every accepted shape.