Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diffStyleUpdates does not reload layers #146

Open
Tenchi2xh opened this issue Apr 10, 2024 · 3 comments
Open

diffStyleUpdates does not reload layers #146

Tenchi2xh opened this issue Apr 10, 2024 · 3 comments

Comments

@Tenchi2xh
Copy link
Contributor

Hi, thanks for the great library!

I noticed that when using diffStyleUpdates set to true and changing the style, it seems that no style.load event is generated by maplibre, resulting in all the layers not being reloaded after getting nuked.

@dimfeld
Copy link
Owner

dimfeld commented Apr 10, 2024

I suspect that fixing #138 will make things work properly here.

@Tenchi2xh
Copy link
Contributor Author

Tenchi2xh commented Apr 15, 2024

I suspect that fixing #138 will make things work properly here.

Great, thanks! A slightly modified version of the fix mentioned in that thread worked for me:

    $: map?.setStyle($isDarkTheme ? darkTheme : lightTheme, {
        transformStyle: (previousStyle, nextStyle) => {
            if (previousStyle) {
                const customLayers = previousStyle.layers.filter(l =>
                    ["line-", "fill-", "heatmap-"].some(prefix => l.id.startsWith(prefix))
                )
                const layers = nextStyle.layers.concat(customLayers)
                const sources = nextStyle.sources

                for (const [key, value] of Object.entries(previousStyle.sources || {})) {
                    if (key.startsWith("geojson-") || key.startsWith("heatmap")) {
                        sources[key] = value
                    }
                }

                return {
                    ...nextStyle,
                    sources: sources,
                    layers: layers,
                }
            }
            return nextStyle
        },
    })

Theme switching is now instant and works pretty well! Before that, without diffStyleUpdates it would take a few seconds because all the layers needed to be re-created.

I also needed to set the style prop of the MapLibre component to an empty string to avoid a DOMException: The user aborted a request in the console

@dimfeld
Copy link
Owner

dimfeld commented Apr 17, 2024

That’s great! Thanks for sharing your solution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants