@suman-malik-repo/snapcache
v1.0.3
Published
Express middleware for SHA256 ETag hashing, 304 responses, and cache acceleration
Maintainers
Readme
@suman-malik-repo/snapcache
Express middleware for SHA256 ETag hashing and cache acceleration. Pairs with the SnapCache CDN script for instant client-side caching.
Install
npm install @suman-malik-repo/snapcacheUsage
Global Middleware
const express = require('express');
const snapCache = require('@suman-malik-repo/snapcache');
const app = express();
// Auto ETag for all GET responses
app.use(snapCache({
exclude: ['/auth', '/admin'],
cacheControl: 'private, no-cache'
}));
app.get('/api/users', (req, res) => {
res.json({ users: [...] }); // ETag auto-added
});Per-Route Helpers
app.get('/api/user/:id', async (req, res) => {
const user = await db.getUser(req.params.id);
res.snapCache(user); // or res.sendWithETag(user)
});Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| exclude | string[] | [] | Routes to skip |
| cacheControl | string | 'private, no-cache' | Cache-Control header |
| weak | boolean | false | Use weak ETags |
How It Works
- Server computes
SHA256(JSON.stringify(data))→ ETag - Client sends
If-None-Match: "etag"on repeat requests - If match →
304 Not Modified(no body sent) - If different →
200with new data + new ETag
Client SDK
Add the CDN script to your frontend for IndexedDB caching + SWR:
<script src="https://snapcache.ogensync.com/sdk.js"></script>License
MIT
