Skip to content

fix(cache): only cache 2xx success responses in cache middleware - #1701

Open
singhanurag0317-bit wants to merge 2 commits into
souma9830:masterfrom
singhanurag0317-bit:feat/issue-810
Open

fix(cache): only cache 2xx success responses in cache middleware#1701
singhanurag0317-bit wants to merge 2 commits into
souma9830:masterfrom
singhanurag0317-bit:feat/issue-810

Conversation

@singhanurag0317-bit

Copy link
Copy Markdown

🐛 Fix: Caching middleware incorrectly caches error response JSONs

Fixes #810

Problem

cacheMiddleware in backend/src/middleware/cache.middleware.js intercepted res.json and cached every response unconditionally. When a database timeout or validation failure occurred, the error response was cached and later served to subsequent valid requests.

Change

  • backend/src/middleware/cache.middleware.js: the res.json override now caches only when res.statusCode is in the success range (200–299). Error responses (4xx/5xx) pass through to the client without being cached.

Tests

  • New backend/src/middleware/__tests__/cacheMiddleware.test.js (4 tests):
    • caches successful 2xx responses (served from cache on the next request),
    • does not cache 4xx validation errors,
    • does not cache 5xx database-timeout errors,
    • leaves non-GET requests untouched.

Verification

  • npm test in backend/ — 9 suites, 24 tests, all passing.

@vercel

vercel Bot commented Aug 3, 2026

Copy link
Copy Markdown

@singhanurag0317-bit is attempting to deploy a commit to the souma9830's projects Team on Vercel.

A member of the Team first needs to authorize it.

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.

Caching middleware incorrectly caches error response JSONs

1 participant