protomaps / PMTiles

Cloud-optimized + compressed single-file tile archives for vector and raster maps
https://protomaps.com/docs/pmtiles/
BSD 3-Clause "New" or "Revised" License
1.84k stars 102 forks source link

cannot set fetch options with maplibre protocol #397

Open eknowles opened 1 month ago

eknowles commented 1 month ago

When using a custom maplibre.addSource to use pmtiles, the PMTiles.getBytes method uses the global fetch method, this is done during the read metadata stage I think.

This means users can not alter the options such as setting credentials

https://github.com/protomaps/PMTiles/blob/main/js/index.ts#L379-L399

Once this has been read maplibre (i think) will then use the loaders.gl fetch methods which does allow the user to override this at a global level.

https://loaders.gl/docs/modules/core/api-reference/fetch-file

Could the pmtiles protocol also use this method so that users can fetch pmtiles using cors + credentials?

bdon commented 1 month ago

You can initialize a FetchSource with CustomHeaders:

https://protomaps.github.io/PMTiles/typedoc/classes/FetchSource.html

eknowles commented 1 month ago

Thank you for the quick response, how would that work with addSource?

maplibre expects a string here both for tiles and url

bdon commented 1 month ago

See this example: https://github.com/protomaps/PMTiles/blob/main/js/examples/maplibre.html#L26

You can then reference it in a style via the same Url and it will resolve to the same instance

new pmtiles.PMTiles(fetchSource)