Express/Connect middleware that sets HTTP response headers to disable client-side caching
npx @tessl/cli install tessl/npm-nocache@4.0.0Nocache is a lightweight Express.js/Connect middleware that sets HTTP response headers to disable client-side caching. It provides a comprehensive approach to cache prevention by setting three key cache-busting headers, making it ideal for web applications that need to ensure users receive up-to-date resources.
npm install nocacheconst nocache = require("nocache");For ES modules:
import nocache from "nocache";TypeScript (with CommonJS interop):
import nocache = require("nocache");Alternative TypeScript import:
import * as nocache from "nocache";const express = require("express");
const nocache = require("nocache");
const app = express();
// Apply nocache middleware to all routes
app.use(nocache());
// Alternative: Apply to specific routes
app.get("/dynamic-content", nocache(), (req, res) => {
res.json({ timestamp: Date.now() });
});
app.listen(3000);Creates Express/Connect middleware that sets cache-busting HTTP headers.
/**
* Creates middleware that sets HTTP headers to disable client-side caching
* @returns {Function} Express/Connect middleware function
*/
function nocache(): (_req: IncomingMessage, res: ServerResponse, next: () => void) => void;The returned middleware function signature:
/**
* Express/Connect middleware that sets cache-busting headers
* @param {IncomingMessage} _req - HTTP request object (unused)
* @param {ServerResponse} res - HTTP response object
* @param {Function} next - Callback to continue middleware chain
*/
function middleware(_req: IncomingMessage, res: ServerResponse, next: () => void): void;Headers Set:
The middleware sets three specific HTTP response headers:
Surrogate-Control: no-store - Instructs surrogate caches (CDNs, reverse proxies) not to store the responseCache-Control: no-store, no-cache, must-revalidate, proxy-revalidate - Comprehensive cache control directive
no-store: Don't store the response in any cacheno-cache: Must revalidate with origin server before using cached responsemust-revalidate: Must revalidate stale responses with origin serverproxy-revalidate: Proxy caches must revalidate stale responsesExpires: 0 - Sets expiration date to past time for legacy cache supportUsage Examples:
const express = require("express");
const nocache = require("nocache");
const app = express();
// Global application of nocache
app.use(nocache());
// Route-specific application
app.get("/api/current-time", nocache(), (req, res) => {
res.json({
timestamp: Date.now(),
message: "This response will not be cached"
});
});
// Selective application for dynamic content
app.use("/api", nocache()); // All API routes
app.use(express.static("public")); // Static files can be cachedFramework Compatibility:
(req, res, next) => void middleware signatureimport { IncomingMessage, ServerResponse } from "http";
/**
* Main nocache function that creates cache-busting middleware
*/
declare const nocache: () => (
_req: IncomingMessage,
res: ServerResponse,
next: () => void
) => void;
export = nocache;