Skip to content

Add a shared SWR-style in-flight request dedupe layer to apiRequest GET calls #301

Description

@Jagadeeshftw

📌 Description

useOptimisticData caches per hook instance via its own cacheRef, so two components mounting simultaneously (e.g. DiscoverPage and a nav widget both calling getLandingStats/getPublicProjects) each fire their own request. Add a small in-flight dedupe map keyed by endpoint+query at the apiRequest GET layer so concurrent identical GETs share a single promise. This reduces duplicate network traffic without a heavy data-fetching library.

💡 Why it matters: Independent components requesting the same public data multiply network load and backend pressure.

🧩 Requirements and context

  • Maintain a module-level Map<string, Promise<unknown>> for in-flight GETs.
  • Key on method+url; only dedupe idempotent GET/HEAD.
  • Clear the map entry on settle (resolve or reject).
  • Never dedupe authenticated mutating requests.
  • Make dedupe opt-out via an option for callers that need a fresh request.

Non-functional requirements

  • Must be secure, tested, and documented.
  • Should be efficient and easy to review.

🛠️ Suggested execution

1. Fork the repo and create a branch

git checkout -b perf/api-inflight-dedupe

2. Implement changes

  • Write/modify the relevant source: src/shared/api/client.ts
  • Write comprehensive tests: src/shared/api/client.test.ts
  • Add documentation: inline TSDoc on the dedupe behavior
  • Include TSDoc doc comments
  • Validate security assumptions: auth and abort signals respected per caller

3. Test and commit

  • Run tests:
npm test -- client
  • Cover edge cases: concurrent identical GETs share a promise; rejection clears the entry; aborts isolated per caller
  • Include test output and security notes in the PR description.

Example commit message

perf(api): dedupe concurrent identical GET requests

✅ Acceptance criteria

  • Concurrent identical GETs issue one fetch
  • Map entry cleared on settle
  • Mutations never deduped
  • Opt-out option works

🔒 Security notes

Only dedupe by URL+method for unauthenticated GETs to avoid leaking one user's response to another via shared promises.

📋 Guidelines

  • Minimum 95% test coverage
  • Clear documentation
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

Labels

GrantFox OSSGrantFox open-source programMaybe RewardedGrantFox: potentially rewarded contributionOfficial CampaignGrantFox official campaign issuearchitectureArchitecture / structural changefrontendFrontend / UI workperformancePerformance / gas / resource use

Fields

No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions