feat: use hosted default_style.json instead of hardcoded layer styling#361
feat: use hosted default_style.json instead of hardcoded layer styling#361Pbhacks wants to merge 4 commits intovalhalla:masterfrom
Conversation
Fixes valhalla#345 Instead of hardcoding Valhalla layer styles in the app (which will become stale), the app now fetches layer definitions at runtime from the upstream hosted default_style.json in the valhalla repository. Changes: - Add fetchValhallaLayers() that fetches from raw.githubusercontent.com/valhalla/valhalla/master/docs/docs/api/tile/default_style.json - Layer IDs and source references are remapped to match the app conventions - Fetched layers are cached per session to avoid redundant network requests - Falls back to hardcoded layers if the remote fetch fails - Updated valhalla-layers-toggle to use async fetchValhallaLayers() - Updated tests with full coverage for the new fetch behavior
There was a problem hiding this comment.
Pull request overview
This PR replaces hardcoded Valhalla MapLibre layer styling with runtime-fetched layer definitions from Valhalla’s upstream default_style.json, while keeping hardcoded layers as a fallback.
Changes:
- Added
fetchValhallaLayers()to fetch and adapt upstream layers, with a session cache and hardcoded fallback. - Updated
ValhallaLayersToggleto asynchronously fetch layers before adding/removing them. - Extended unit tests to cover the new fetch behavior and async toggle behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/components/tiles/valhalla-layers.ts | Adds remote style URL constant, layer adaptation, session cache, and async fetch with fallback to hardcoded layers. |
| src/components/tiles/valhalla-layers.spec.ts | Adds tests for the default style URL and fetch/fallback behavior. |
| src/components/tiles/valhalla-layers-toggle.tsx | Switches to async layer fetching and stores fetched layers for later removal. |
| src/components/tiles/valhalla-layers-toggle.spec.tsx | Mocks fetchValhallaLayers() and updates assertions to wait for async behavior. |
Comments suppressed due to low confidence (2)
src/components/tiles/valhalla-layers-toggle.tsx:59
- In the toggle-off branch, the code removes every layer in
layersRef.currentif it exists on the map, even if that layer pre-existed and was not added by this toggle (note the toggle-on branch skipsaddLayerwhen a layer already exists). This can accidentally remove layers owned by other parts of the app if IDs overlap. Track which layer IDs were actually added (e.g., push to a ref/set only whenaddLayeris called) and only remove those on toggle-off.
for (const layer of layers) {
if (!map.getLayer(layer.id)) {
map.addLayer(layer);
}
}
} else {
for (const layer of layersRef.current) {
if (map.getLayer(layer.id)) {
map.removeLayer(layer.id);
}
}
src/components/tiles/valhalla-layers-toggle.tsx:63
map.removeSource(VALHALLA_SOURCE_ID)is called whenever the toggle is switched off, even if the source already existed before toggling on (the toggle-on branch conditionally callsaddSource). This can remove a source that another component expects to remain. Similar to layers, track whether this toggle added the source (e.g.,addedSourceRef.current = trueonly when callingaddSource) and only remove it when it was added here.
if (!map.getSource(VALHALLA_SOURCE_ID)) {
map.addSource(VALHALLA_SOURCE_ID, getValhallaSourceSpec());
}
for (const layer of layers) {
if (!map.getLayer(layer.id)) {
map.addLayer(layer);
}
}
} else {
for (const layer of layersRef.current) {
if (map.getLayer(layer.id)) {
map.removeLayer(layer.id);
}
}
if (map.getSource(VALHALLA_SOURCE_ID)) {
map.removeSource(VALHALLA_SOURCE_ID);
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
nilsnolde
left a comment
There was a problem hiding this comment.
same here. follow the guidelines. also you can see that @lourduradjou already was asking in #345. it'd be the sensible thing to first claim the issue, but I'll let this slide bcs it's kinda stale after 3 days.
|
Hi apologies @nilsnolde for not claiming it before, I will keep that in mind in future issues and do accordingly. Thanks for allowing me |
- Move VALHALLA_DEFAULT_STYLE_URL to .env (VITE_VALHALLA_DEFAULT_STYLE_URL) - Don't cache fallback layers so transient failures can be retried - Export _resetLayerCache() and use it in tests for proper isolation - Add abort guard (toggleIdRef) to prevent stale async fetches from mutating the map after rapid toggle on/off
|
Preview is ready! 🚀 You can view it here: https://valhalla-app-tests.gis-ops.com/361 |
Fixes #345
Instead of hardcoding Valhalla layer styles in the app (which will become stale), the app now fetches layer definitions at runtime from the upstream hosted default_style.json in the valhalla repository.
Changes:
🛠️ Fixes Issue
Closes #345
📄 Note to reviewers
I have made necessary changes kindly let me know if it is good.