Kind: Function
Source: src/middleware/etag/index.ts
Part of: Middleware
ETag Middleware for Hono.
etag creates an ETag header from a response body after downstream handlers run. When the request includes a matching If-None-Match header, it replaces the response with a Not Modified response.
Signature
tsfunction etag(options: ETagOptions): MiddlewareHandler
Parameters
| Name | Type |
|---|---|
options | ETagOptions |
Returns: MiddlewareHandler
Diagram
mermaidgraph LR Request --> ETagMiddleware ETagMiddleware --> Handler Handler --> Response Response --> ETagMiddleware ETagMiddleware -->|Sets ETag header| Client ETagMiddleware -->|Matching If-None-Match| NotModifiedResponse
Usage
tsimport { Hono } from 'hono'
import { etag } from 'hono/etag'
const app = new Hono()
app.use(etag())
app.get('/profile', (c) => {
return c.json({ name: 'Ada' })
})
export default app
AI Coding Instructions
- Register
etag()before routes whose responses should include anETagheader. - Allow downstream handlers to create the response before adding logic that depends on response content.
- Preserve an
ETagheader set explicitly by a route or another middleware. - Test conditional requests with an
If-None-Matchheader that matches the responseETag.
Was this page helpful?