Skip to content

feat: cache viewport bounds, debounce fetches, dedup polygons#58

Open
madhavcodez wants to merge 1 commit intomainfrom
fix/fetch-storm-and-caching
Open

feat: cache viewport bounds, debounce fetches, dedup polygons#58
madhavcodez wants to merge 1 commit intomainfrom
fix/fetch-storm-and-caching

Conversation

@madhavcodez
Copy link
Copy Markdown
Contributor

What this does

Stops the dashboard from spamming the backend every time the map moves, and stops damage polygons from flickering in and out on each fetch.

Three mechanisms:

  1. Bounds cache (useBoundsCache) — tracks the union of regions already fetched. When the current viewport sits inside that cached region, no API call fires at all. When a fetch is needed, bounds expand by 1.5× so pre-loaded surroundings absorb small pans.
  2. Debounce (useDebouncedCallback) — viewport changes are collapsed into a single fetch after 300ms of no movement, so rapid zig-zag panning doesn't produce 50 parallel requests.
  3. Polygon dedup — polygons are merged into a Map keyed by id, so new fetches never replace what's already drawn. No more flicker.

Safety fixes

  • The bounds cache only advances after a fetch succeeds — an aborted or failed fetch won't silently mark a region as "cached with nothing in it."
  • The polygon map is capped at 20,000 entries, trimmed to 15,000 when exceeded, so memory and render cost stay bounded across a long session.

How to verify

DevTools → Network → filter locations:

  • Pan inside an already-fetched area → zero new requests
  • Fast pan across a new area → one request per ~300ms (debounce)
  • Pan far, then back → polygons you already saw are still on the map (dedup)

Closes #56
Closes #43
Closes #54

Add useBoundsCache (check, commit, reset) and useDebouncedCallback
hooks. Dashboard now skips /locations fetches when the viewport sits
inside a previously fetched region expanded 1.5x, debounces viewport
changes to 300ms so fast panning collapses into a single request,
and merges polygons into a Map keyed by id so redraws never flicker
out already-visible buildings.

The bounds cache only advances after a fetch succeeds, so aborted
requests do not silently leave regions marked cached with no data.
The polygon map is capped at 20,000 entries, trimmed to the most
recent 15,000 when exceeded.

Closes #56
Closes #43
Closes #54
@madhavcodez madhavcodez self-assigned this Apr 14, 2026
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 14, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
frontend Error Error Apr 14, 2026 3:57pm

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.

Don't fetch images/data that is already stored Expand area that gets loaded based on view frontend caching?

1 participant