or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-nocache

Express/Connect middleware that sets HTTP response headers to disable client-side caching

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/nocache@4.0.x

To install, run

npx @tessl/cli install tessl/npm-nocache@4.0.0

index.mddocs/

Nocache

Nocache 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.

Package Information

  • Package Name: nocache
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Installation: npm install nocache
  • Node.js Requirements: >=16.0.0

Core Imports

const 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";

Basic Usage

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);

Capabilities

Nocache Middleware Factory

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:

  1. Surrogate-Control: no-store - Instructs surrogate caches (CDNs, reverse proxies) not to store the response
  2. Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate - Comprehensive cache control directive
    • no-store: Don't store the response in any cache
    • no-cache: Must revalidate with origin server before using cached response
    • must-revalidate: Must revalidate stale responses with origin server
    • proxy-revalidate: Proxy caches must revalidate stale responses
  3. Expires: 0 - Sets expiration date to past time for legacy cache support

Usage 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 cached

Framework Compatibility:

  • Express.js
  • Connect
  • Any framework that accepts (req, res, next) => void middleware signature

Types

import { IncomingMessage, ServerResponse } from "http";

/**
 * Main nocache function that creates cache-busting middleware
 */
declare const nocache: () => (
  _req: IncomingMessage, 
  res: ServerResponse, 
  next: () => void
) => void;

export = nocache;