AlexLavoie42 / Nuxt-Mapbox

Elegant Mapbox integration with Nuxt
81 stars 11 forks source link

Layers lost after changing styles #95

Closed Timo972 closed 6 months ago

Timo972 commented 6 months ago

Hi, I use the MapSource and MapLayer components to register mapbox sources and layers. Facing the issue that they are being unloaded on a style change, i did some research on why's that. Interestingly this is intended behaviour by mapbox, as they even provide an example for persistent sources/layers. See https://docs.mapbox.com/mapbox-gl-js/example/style-switch/

As far as i can tell, this logic is already implemented for sources but is missing for layers. Is there a specific reason this is the case?

Implementing this enhancement should be as easy as calling addLayer in the style load event callback over here, similar to the implementation in the MapSource component.

I can do a pr for this 3-liner if you like 😉

AlexLavoie42 commented 6 months ago

In theory this line should be reloading the layers on style change, although perhaps the source isn't reloaded or this doesn't get fired in some cases.

https://github.com/AlexLavoie42/Nuxt-Mapbox/blob/cdf05af03cd745f9c664b9cfb965221917e71a68/src/runtime/components/Layer.vue#L66

In fact in the playground the original layer persists when I change style. The only bug I notice is that if I change layers it gets reset to the original.

Doing as you are saying and copying the implementation from the source into the layer might work. You will probably need to change the addLayer function to not add a layer if it already exists like it does for the source.

https://github.com/AlexLavoie42/Nuxt-Mapbox/blob/cdf05af03cd745f9c664b9cfb965221917e71a68/src/runtime/components/Source.vue#L21-L31

I might take a look into this later when I have time (CI/CD is currently broken which is blocking the latest Mapbox update so I need to fix that first.) Feel free to give this a try if you would like, a PR is always welcome!

AlexLavoie42 commented 6 months ago

Now that I think about it, if the layer is loaded after the source this does not get called. Perhaps the proper fix is to just make sure this always gets called so the layer always gets reloaded with the source.

https://github.com/AlexLavoie42/Nuxt-Mapbox/blob/cdf05af03cd745f9c664b9cfb965221917e71a68/src/runtime/components/Layer.vue#L64-L68

Timo972 commented 6 months ago

Seems like this issue got more depth than expected. I will have a deeper look at it later on, so you can focus fixing the CI.

Timo972 commented 6 months ago

Now that I think about it, if the layer is loaded after the source this does not get called. Perhaps the proper fix is to just make sure this always gets called so the layer always gets reloaded with the source.

https://github.com/AlexLavoie42/Nuxt-Mapbox/blob/cdf05af03cd745f9c664b9cfb965221917e71a68/src/runtime/components/Layer.vue#L64-L68

Guess you're right, I will give it a try

Timo972 commented 6 months ago

Now that I think about it, if the layer is loaded after the source this does not get called. Perhaps the proper fix is to just make sure this always gets called so the layer always gets reloaded with the source.

https://github.com/AlexLavoie42/Nuxt-Mapbox/blob/cdf05af03cd745f9c664b9cfb965221917e71a68/src/runtime/components/Layer.vue#L64-L68

implemented in pr #96