Skip to content

feat: use hosted default_style.json instead of hardcoded layer styling#361

Open
Pbhacks wants to merge 4 commits intovalhalla:masterfrom
Pbhacks:fix/issue-345-use-hosted-default-style
Open

feat: use hosted default_style.json instead of hardcoded layer styling#361
Pbhacks wants to merge 4 commits intovalhalla:masterfrom
Pbhacks:fix/issue-345-use-hosted-default-style

Conversation

@Pbhacks
Copy link

@Pbhacks Pbhacks commented Mar 8, 2026

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:

  • 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

🛠️ Fixes Issue

Closes #345

📄 Note to reviewers

I have made necessary changes kindly let me know if it is good.

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
Copilot AI review requested due to automatic review settings March 8, 2026 13:24
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ValhallaLayersToggle to 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.current if it exists on the map, even if that layer pre-existed and was not added by this toggle (note the toggle-on branch skips addLayer when 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 when addLayer is 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 calls addSource). 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 = true only when calling addSource) 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.

Copy link
Member

@nilsnolde nilsnolde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Pbhacks
Copy link
Author

Pbhacks commented Mar 9, 2026

Hi apologies @nilsnolde for not claiming it before, I will keep that in mind in future issues and do accordingly. Thanks for allowing me

Pbhacks and others added 2 commits March 9, 2026 06:50
- 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
@github-actions
Copy link

Preview is ready! 🚀 You can view it here: https://valhalla-app-tests.gis-ops.com/361

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

Successfully merging this pull request may close these issues.

use hosted default_style.json instead of manual styling

3 participants