cacheable-request provides RFC 7234 compliant HTTP caching for Node.js's native HTTP and HTTPS modules. It is a low-level wrapper, not a high-level request library, designed to add caching capabilities directly to `http.request` or `https.request`. The current stable version is 13.0.18, with frequent patch and minor updates, and occasional breaking changes between major versions as seen in recent changelogs. Key differentiators include its strict adherence to RFC 7234 for cache validation and storage logic, out-of-the-box in-memory caching, and a highly pluggable architecture for various storage adapters, prominently featuring Keyv for flexible backend integration. It handles fresh and stale cache entries, revalidation with `If-None-Match`/`If-Modified-Since`, and 304 responses, updating the `Age` header accordingly.
npm install cacheable-requestVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to wrap Node.js native `http.request` with `cacheable-request` to add RFC-compliant caching. It includes a small server to serve cacheable content and then performs multiple requests to show caching in action, distinguishing between initial requests and cached responses.
Initialize `Keyv` separately and pass the instance: `import Keyv from '@keyv/redis'; const cache = new Keyv('redis://...'); new CacheableRequest(http.request, { cache }).request();`Change `const cacheableRequest = new CacheableRequest(http.request);` to `const cacheableRequest = new CacheableRequest(http.request).request();`
Migrate your project to use ES Modules (e.g., set `"type": "module"` in `package.json` and use `import`). For existing CommonJS projects, consider staying on v8 or lower, or use dynamic `import()` for `cacheable-request`.
Understand that you will still interact with Node's native `IncomingMessage` and `ClientRequest` objects. If you need a higher-level API, integrate `cacheable-request` with a compatible client library or use a different client that offers built-in caching.
Review your cache eviction strategies if using `node-cache` storage. Consider implementing alternative memory management or switching to a Keyv adapter with explicit size limits if `maxKeys` functionality is critical.
Update your project to use ES Modules (e.g., `"type": "module"` in `package.json` and `import CacheableRequest from 'cacheable-request';`) or use dynamic `import()` if you must remain in a CommonJS context: `const { default: CacheableRequest } = await import('cacheable-request');`Change `const cacheableRequest = new CacheableRequest(http.request);` to `const cacheableRequest = new CacheableRequest(http.request).request();`
Instantiate Keyv with the connection string first, then pass the Keyv instance to `CacheableRequest`: `const cache = new Keyv('redis://localhost:6379'); const cacheableRequest = new CacheableRequest(http.request, { cache }).request();`Ensure you are using the correct function signature, or cast the argument if you are certain of its compatibility, e.g., `cacheableRequest('http://example.com', { /* options */ }, callback);` or ensure the `request` function is correctly typed if you've wrapped it.