Express/Connect middleware that sets HTTP response headers to disable client-side caching
npx @tessl/cli install tessl/npm-nocache@4.0.00
# Nocache
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: nocache
7
- **Package Type**: npm
8
- **Language**: JavaScript (with TypeScript definitions)
9
- **Installation**: `npm install nocache`
10
- **Node.js Requirements**: >=16.0.0
11
12
## Core Imports
13
14
```javascript
15
const nocache = require("nocache");
16
```
17
18
For ES modules:
19
20
```javascript
21
import nocache from "nocache";
22
```
23
24
TypeScript (with CommonJS interop):
25
26
```typescript
27
import nocache = require("nocache");
28
```
29
30
Alternative TypeScript import:
31
32
```typescript
33
import * as nocache from "nocache";
34
```
35
36
## Basic Usage
37
38
```javascript
39
const express = require("express");
40
const nocache = require("nocache");
41
42
const app = express();
43
44
// Apply nocache middleware to all routes
45
app.use(nocache());
46
47
// Alternative: Apply to specific routes
48
app.get("/dynamic-content", nocache(), (req, res) => {
49
res.json({ timestamp: Date.now() });
50
});
51
52
app.listen(3000);
53
```
54
55
## Capabilities
56
57
### Nocache Middleware Factory
58
59
Creates Express/Connect middleware that sets cache-busting HTTP headers.
60
61
```javascript { .api }
62
/**
63
* Creates middleware that sets HTTP headers to disable client-side caching
64
* @returns {Function} Express/Connect middleware function
65
*/
66
function nocache(): (_req: IncomingMessage, res: ServerResponse, next: () => void) => void;
67
```
68
69
The returned middleware function signature:
70
71
```javascript { .api }
72
/**
73
* Express/Connect middleware that sets cache-busting headers
74
* @param {IncomingMessage} _req - HTTP request object (unused)
75
* @param {ServerResponse} res - HTTP response object
76
* @param {Function} next - Callback to continue middleware chain
77
*/
78
function middleware(_req: IncomingMessage, res: ServerResponse, next: () => void): void;
79
```
80
81
**Headers Set:**
82
83
The middleware sets three specific HTTP response headers:
84
85
1. **`Surrogate-Control: no-store`** - Instructs surrogate caches (CDNs, reverse proxies) not to store the response
86
2. **`Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate`** - Comprehensive cache control directive
87
- `no-store`: Don't store the response in any cache
88
- `no-cache`: Must revalidate with origin server before using cached response
89
- `must-revalidate`: Must revalidate stale responses with origin server
90
- `proxy-revalidate`: Proxy caches must revalidate stale responses
91
3. **`Expires: 0`** - Sets expiration date to past time for legacy cache support
92
93
**Usage Examples:**
94
95
```javascript
96
const express = require("express");
97
const nocache = require("nocache");
98
99
const app = express();
100
101
// Global application of nocache
102
app.use(nocache());
103
104
// Route-specific application
105
app.get("/api/current-time", nocache(), (req, res) => {
106
res.json({
107
timestamp: Date.now(),
108
message: "This response will not be cached"
109
});
110
});
111
112
// Selective application for dynamic content
113
app.use("/api", nocache()); // All API routes
114
app.use(express.static("public")); // Static files can be cached
115
```
116
117
**Framework Compatibility:**
118
119
- Express.js
120
- Connect
121
- Any framework that accepts `(req, res, next) => void` middleware signature
122
123
## Types
124
125
```typescript { .api }
126
import { IncomingMessage, ServerResponse } from "http";
127
128
/**
129
* Main nocache function that creates cache-busting middleware
130
*/
131
declare const nocache: () => (
132
_req: IncomingMessage,
133
res: ServerResponse,
134
next: () => void
135
) => void;
136
137
export = nocache;
138
```