@arraypress/edge-cache
v1.0.0
Published
Tiny Hono middleware that attaches Cache-Control headers to successful GETs so CDNs (Cloudflare, Fastly, etc.) absorb traffic the origin would otherwise handle. Dev-mode is no-op.
Maintainers
Readme
@arraypress/edge-cache
Tiny Hono middleware that attaches Cache-Control headers to successful GETs so CDNs (Cloudflare, Fastly, Vercel) absorb repeat anonymous traffic the origin would otherwise handle.
Install
npm install @arraypress/edge-cacheUsage
import { edgeCache } from '@arraypress/edge-cache';
// Public store endpoints — cache at the edge for 5 minutes.
app.use('/api/store/*', edgeCache({ maxAge: 0, sMaxAge: 300 }));
// Branding config rarely changes — longer TTL is fine.
app.use('/api/public/branding', edgeCache({ maxAge: 0, sMaxAge: 3600 }));Skip conditions
The middleware never sets Cache-Control when any of these are true:
c.env.ENVIRONMENT === 'development'(override viadevEnvVar/devEnvValue)- Non-GET request
- Response status ≠ 200
- Downstream handler has already set
Cache-Control
This is deliberate: dev-mode edits should be immediately visible; error responses should never pin at the edge; per-route opt-outs work by just setting a conflicting header in the handler.
Options
| Option | Default | What |
|---|---|---|
| maxAge | 0 | Browser cache seconds. 0 means don't cache at the browser (admin edits always visible to the authoring user). |
| sMaxAge | 60 | Edge cache seconds. |
| directive | 'public' | Leading directive. Change to 'private' for per-user caching or 'public, must-revalidate' for stale-while-revalidate semantics. |
| devEnvVar | 'ENVIRONMENT' | Env var name checked for the dev skip. |
| devEnvValue | 'development' | Value that marks dev mode. |
Why this exists
Every Hono-on-Workers app writes this exact middleware. Setting s-maxage=60 on anonymous GET endpoints is the single most effective lever on D1 read quota — once the CDN absorbs repeat hits, the origin only handles unique-user traffic.
License
MIT
