ETag-based HTTP Response Caching

2.0.3 · deprecated · verified Wed Apr 22

request-etag is a small, in-memory module designed for ETag-based HTTP response caching. It manages the `If-None-Match` header automatically for subsequent GET requests to the same URL, retrieving cached bodies for 304 Not Modified responses. The package currently leverages `lru-cache` for its underlying caching mechanism, which can be configured with options like `max` size. Crucially, `request-etag` defaults to using the `request` library as its HTTP client, which has been officially deprecated and is no longer maintained since February 11th, 2020. While an alternative HTTP client can be injected, it *must* adhere to the `request` library's API signature. The package's current stable version is 2.0.3, but its reliance on a deprecated core dependency suggests a slow or inactive release cadence, limiting its suitability for new projects. Its primary differentiator is its focused, lightweight approach to abstracting ETag caching logic.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `request-etag` with a cache configuration and perform ETag-aware GET requests, showing the behavior of both initial 200 OK and subsequent 304 Not Modified responses with cached bodies.

const ETagRequest = require('request-etag');
const request = require('request'); // In a real application, use a modern HTTP client compatible with 'request' API

// Configure the cache, e.g., max 10MB to store response bodies
const cacheConfig = {
    max: 10 * 1024 * 1024 // 10MB limit for the cache
};

// Initialize ETagRequest. In a production environment, 'request' should be replaced 
// with a modern, maintained HTTP client that provides the same API signature.
const eTagRequest = new ETagRequest(cacheConfig, request);

// Define a target URL for caching
const urlToCache = 'https://www.google.com/'; 

console.log('--- First request (expecting 200 OK) ---');
eTagRequest(urlToCache, function (error, response, body) {
    if (error) {
        console.error('Initial request failed:', error);
        return;
    }

    if (response.statusCode === 200) {
        console.log(`Status: ${response.statusCode} - Body retrieved from actual HTTP response.`);
        // console.log(body.substring(0, 100) + '...'); // Log a snippet of the body
    } else {
        console.log(`Initial request unexpected status: ${response.statusCode}`);
    }

    console.log('\n--- Second request (expecting 304 Not Modified from cache) ---');
    eTagRequest(urlToCache, function (error2, response2, body2) {
        if (error2) {
            console.error('Second request failed:', error2);
            return;
        }

        if (response2.statusCode === 304) {
            console.log(`Status: ${response2.statusCode} - Body retrieved from internal cache.`);
            // console.log(body2.substring(0, 100) + '...'); // Log a snippet of the cached body
        } else if (response2.statusCode === 200) {
            console.log(`Status: ${response2.statusCode} - Unexpected 200 on second request, cache might be bypassed.`);
        } else {
            console.log(`Second request unexpected status: ${response2.statusCode}`);
        }
    });
});

view raw JSON →