Skip to content

feat(leaderboard): add ETag/304 caching on /api/leaderboard - #726

Merged
greatest0fallt1me merged 3 commits into
Predictify-org:mainfrom
Obaara293:fix/etag-leaderboard-635
Jul 29, 2026
Merged

feat(leaderboard): add ETag/304 caching on /api/leaderboard#726
greatest0fallt1me merged 3 commits into
Predictify-org:mainfrom
Obaara293:fix/etag-leaderboard-635

Conversation

@Obaara293

Copy link
Copy Markdown
Contributor

Overview

This PR adds strong ETag support to both /api/leaderboard endpoints (GET / and GET /user/:stellarAddress) to enable conditional GET responses — cutting bandwidth on repeat reads via 304 Not Modified.

Related Issue

Closes #635

Changes

Routes

  • [MODIFY] src/routes/leaderboard.ts — added conditionalGet to GET / and GET /user/:stellarAddress:
    • Every 200 response now includes ETag (SHA-256 of the JSON payload) and Cache-Control: no-cache headers
    • When the client sends If-None-Match matching the current ETag, responds with 304 Not Modified (no body)
    • Bare-hash (unquoted) If-None-Match values are handled correctly via existing middleware normalization

Tests

  • [ADD] src/__tests__/routes/leaderboard.test.ts — 10 new integration tests covering:
    • ETag and Cache-Control headers on first request
    • 304 when If-None-Match matches
    • 200 when ETag is stale
    • 304 with unquoted (bare hash) If-None-Match
    • ETag stability across identical payloads
    • ETag changes when payload changes
    • 304 response includes ETag header
    • Same coverage for the user-specific endpoint

Config

  • [MODIFY] jest.config.js — added src/__tests__/**/*.test.ts to test match pattern so co-located tests are discovered

Verification Results

npm test -- src/__tests__/routes/leaderboard.test.ts -t "ETag"
✅ 10/10 passed

PASS src/__tests__/routes/leaderboard.test.ts (19.6s)
  Leaderboard Routes
    ETag / 304 conditional GET for GET /api/leaderboard
      ✓ returns 200 with ETag and Cache-Control on first request
      ✓ returns 304 when If-None-Match matches current ETag
      ✓ returns 200 when If-None-Match does not match
      ✓ returns 304 with unquoted (bare hash) If-None-Match
      ✓ ETag is stable across repeated requests for the same data
      ✓ ETag changes when leaderboard data changes
      ✓ 304 still includes ETag header
    ETag / 304 conditional GET for GET /api/leaderboard/user/:stellarAddress
      ✓ returns 200 with ETag and Cache-Control on first request
      ✓ returns 304 when If-None-Match matches
      ✓ returns 200 when If-None-Match does not match
Acceptance Criteria Status
ETag emitted on GET /api/leaderboard ✅ Strong ETag via SHA-256 hash of JSON payload
ETag emitted on GET /api/leaderboard/user/:stellarAddress ✅ Strong ETag via SHA-256 hash of JSON payload
304 returned when If-None-Match matches ✅ Both endpoints return 304 with no body
Cache-Control: no-cache on every response ✅ Clients may cache but must revalidate
Deterministic ETags (sorted keys) ✅ Uses sortedReplacer from existing etag.ts middleware
10 focused tests added ✅ All pass

- Added conditionalGet from etag middleware to both leaderboard endpoints
- GET / returns 304 when If-None-Match matches the leaderboard payload
- GET /user/:stellarAddress returns 304 when If-None-Match matches the entry payload
- Added comprehensive ETag test coverage (10 tests)
- Updated jest config to also match src/__tests__ tests

Closes Predictify-org#635
@drips-wave

drips-wave Bot commented Jul 28, 2026

Copy link
Copy Markdown

@Obaara293 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@greatest0fallt1me
greatest0fallt1me merged commit cd62858 into Predictify-org:main Jul 29, 2026
1 check passed
@greatest0fallt1me

Copy link
Copy Markdown
Contributor

Green across the board — merging. Thanks!

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.

Add ETag / 304 caching on /api/leaderboard [b#061]

2 participants