Registry / devops / cacheable-response

cacheable-response

JSON →
library2.10.8jsnpmunverified

An HTTP-compliant route path middleware for serving cached responses with invalidation for Node.js. Version 2.10.8 supports Node >=12 and is actively maintained (frequent releases). It provides a framework-agnostic caching layer for SSR, with built-in LRU cache, automatic TTL-based invalidation, and stale-while-revalidate support. Unlike simple in-memory caches, it integrates cache status headers (MISS, HIT, EXPIRED, BYPASS, STALE) and works with any HTTP server framework. Key differentiators: focus on SSR caching, auto-invalidation, and easy integration with Express, Fastify, or raw http.

npm install cacheable-response
INSTALL
IMPORT
SIG · CACHEABLE-RESPONSE
C
cacheable-response
devopsjavascriptv2.10.8
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cacheableResponse
const cacheableResponse = require('cacheable-response')
import cacheableResponse from 'cacheable-response'
Package is CommonJS only; no ESM exports as of v2.10.8. Use require().
cacheableResponse
const ssrCache = require('cacheable-response')({ get, send })
const cacheableResponse = require('cacheable-response'); const ssrCache = cacheableResponse.default({ get, send })
The default export is the factory function; no .default property exists.
cacheableResponse
app.use((req, res) => ssrCache({ req, res }))
app.use(ssrCache)
ssrCache is not a standard middleware function; it must be called manually with req, res object.

Creates a basic HTTP server using cacheable-response as middleware, caching a JSON response for 1 minute.

const http = require('http'); const cacheableResponse = require('cacheable-response'); const ssrCache = cacheableResponse({ get: ({ req, res }) => ({ data: { message: 'Hello World', timestamp: Date.now() }, ttl: 60000 // 1 minute }), send: ({ data, res }) => { res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify(data)); } }); http.createServer((req, res) => ssrCache({ req, res })).listen(3000, () => { console.log('Server running at http://localhost:3000/'); });
Debug
Known issues
gotchaThe `send` function receives an object with `data`, `res`, `req`, and `ttl` properties. Do not call `res.send()` from Express unless you handle Express-specific response methods.
fix
Use `res.end()` or `res.json()` depending on the framework. For Express, `res.json(data)` works if you include `express` context.
affects: >=2.0.0
gotchaThe cache key is derived from the request URL and method by default. If you use query parameters, they are not ignored; this can cause unexpected cache misses.
fix
Provide a custom `cacheKey` function in the options to normalize URLs, e.g., strip query params or sort them.
affects: >=1.0.0
deprecatedThe `compress` option has been deprecated since v2.8.0. It will be removed in a future version.
fix
Remove the `compress` option from the factory options. Compression is now handled externally if needed.
affects: >=2.8.0
gotchaThe `ttl` returned from the `get` function is in milliseconds. A common mistake is to provide seconds or use a TTL that is too short.
fix
Ensure TTL is in milliseconds. For 1 hour, use `3600000`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: cacheableResponse is not a function
Using ES import syntax on a CommonJS module.
fix
Change to `const cacheableResponse = require('cacheable-response')`
TypeError: Cannot destructure property 'data' of 'undefined' or 'null'
The `get` function did not return an object with `data` property; returned undefined or null.
fix
Ensure `get` returns an object like `{ data: ..., ttl: ... }`
Error: listen EADDRINUSE :::3000
Port 3000 is already in use by another process.
fix
Kill the process using the port or change the port number.
Upgrade
Version history
2.10.8latest on npm
Audit
Dependencies
lru-cacherequiredinternal LRU cache implementation
on-headersrequiredcapture response headers for cache
Agent activity
2 hits · last 30 days
node
2
Resources
cacheable-response — npm install cacheable-response · libregistry