Skip to content

perf(api): tune caching, retries, and prefetch pressure #97

Closed

Description

@Ardnived

Summary

The release-readiness review (.claude/reports/RELEASE_READINESS.md §1.2/§1.5/§8, Appendix A — local, gitignored) found the data layer's architecture excellent (fetch-once + stale-while-revalidate, warm-up on boot, quantized keys) but its pressure knobs untouched: a zero default staleTime makes every results-list mount redo real main-thread work, React Query's default 3× retry amplifies load during exactly a SahajCloud outage, hover prefetch can fire one request per row traversed, and the default 5-minute gcTime can evict the observer-less wholesale caches the whole architecture depends on. All four are cheap, local fixes in the query configuration layer.

Current behavior

  • src/config/query-client.ts:7-13 sets only refetchOnWindowFocus: false. The global staleTime is 0, so every mount of the results list re-runs the derived events query — full-feed filter + per-event zod parse + distance sort. No network (the feed is cached) but real main-thread work per remount. eventsQuery (src/config/api/index.ts:37-51) sets no staleTime of its own.
  • React Query's default retry (3× exponential backoff) is untouched anywhere — no retry: configuration exists in src/ (grep-verified). A SahajCloud outage means every client retries every query 3×, amplifying load exactly when the API struggles. No AbortController usage repo-wide.
  • Hover prefetch is unthrottled: src/components/molecules/EventListItem/EventListItem.tsx:84-87 — the shared activate handler fires prefetchEvent(event.id) on every mouseenter/focus. Dragging the cursor down a long list fires one GET /events/:id per row traversed; the keys are distinct per event, so request dedup doesn't help. The report calls this "the most likely accidental request storm" (§1.5).
  • The default 5-min gcTime can evict the observer-less caches: ['regions'], ['geojson'] and ['event-titles', locale] are populated via ensureQueryData in the imperative loaders (src/config/api/fetch.ts:147 loadRegions, :183 loadGeojson, :227 loadEventTitles) which pass staleTime but no gcTime — after idle, the wholesale feed refetches from scratch. GEOJSON_STALE_TIME / REGIONS_STALE_TIME constants already exist (src/config/query-client.ts:17,22) to hang this on.

Proposed changes

  • Set a sane staleTime on the derived events query (eventsQuery) so a filter/list remount inside the window does zero recompute; consider a sensible global default in query-client.ts too.
  • Bound retries globally in query-client.ts: 1–2 retries, capped delay, no retry on 4xx (the error kind/status is already recoverable — classifyError/atlasError in src/lib/report.ts carry it; a 4xx retry fails identically).
  • Throttle hover prefetch: a small dwell delay (~150 ms hover-intent timer) and/or a concurrency cap in usePrefetchEvent (src/hooks/use-prefetch-event.ts) / the activate handler region of EventListItem.tsx. Extract the dwell timer as a pure helper so it's unit-testable without a DOM.
  • Pin gcTime (and confirm staleTime) for the observer-less ['regions'] / ['geojson'] / ['event-titles'] caches, using the existing constants. Keep the loaders on ensureQueryData({ revalidateIfStale: true }) — do NOT switch them to fetchQuery (it blocks on the stale refetch; see .claude/rules/data-layer.md).

Acceptance criteria

  • A filter/list remount within the stale window does zero recompute of the events query — verified by a query-fn call counter in a unit test.
  • Retry config is bounded: at most 1–2 retries, capped delay, and none for 4xx responses — covered by a unit test on the retry predicate.
  • A hover sweep across N rows fires ≤ a bounded number of prefetches — unit-tested with the dwell timer extracted pure (fake timers, no jsdom needed if the timer logic is pure).
  • ['regions'] and ['event-titles', locale] survive an idle period longer than the default 5-min gcTime (pinned via the existing constants); the imperative loaders remain ensureQueryData + revalidateIfStale: true.
  • No regression: a locale switch still refetches only the titles sliver (locale stays in its key); the touch-device idle warm-up (usePrefetchEvents, EAGER_COUNT = 3) still fires; hover prefetch still warms before a deliberate click (dwell ≪ typical hover-to-click time).
  • pnpm lint && pnpm typecheck && pnpm test:run pass.
  • Write an implementation report to .claude/reports/<issue-number>-query-pressure.md — in the MAIN checkout at /Users/devindra/Documents/Projects/SahajAtlasWeb even when implementing from a worktree (the folder is gitignored) — covering what changed, what was deliberately NOT changed, and which readiness-report findings are resolved.

Files affected

  • src/config/query-client.ts
  • src/config/api/index.ts (query factories)
  • src/hooks/use-prefetch-event.ts
  • src/components/molecules/EventListItem/EventListItem.tsx — the activate/prefetch handlers region only

Parallel implementation: this ticket OWNS the files above; sibling release-hardening tickets run concurrently. Avoid edits outside owned files except strictly additive ones — e.g. adding a gcTime option to the ensureQueryData calls in src/config/api/fetch.ts:147/183/227 counts as strictly additive and is permitted; restructuring those loaders is not.

References

  • .claude/reports/RELEASE_READINESS.md §1.2, §1.5, §1.6 (recommendation 3), §8, Appendix A (local, gitignored)
  • Data-layer caching rules: .claude/rules/data-layer.md (stale-while-revalidate imperative reads; don't switch to fetchQuery)
  • Error classification for the 4xx retry predicate: src/lib/report.ts (classifyError, issue feat(widget): classify errors and show only the actions that can help #89)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Hold Until

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions