# etag

**Kind:** Function

**Source:** [`src/middleware/etag/index.ts`](https://github.com/honojs/hono/blob/main/src/middleware/etag/index.ts#L79)

**Part of:** [Middleware](subsystem-src-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

```ts
function etag(options: ETagOptions): MiddlewareHandler
```

## Parameters

| Name | Type |
|---|---|
| `options` | `ETagOptions` |

**Returns:** `MiddlewareHandler`

## Diagram

```mermaid
graph LR
  Request --> ETagMiddleware
  ETagMiddleware --> Handler
  Handler --> Response
  Response --> ETagMiddleware
  ETagMiddleware -->|Sets ETag header| Client
  ETagMiddleware -->|Matching If-None-Match| NotModifiedResponse
```

## Usage

```ts
import { 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 an `ETag` header.
- Allow downstream handlers to create the response before adding logic that depends on response content.
- Preserve an `ETag` header set explicitly by a route or another middleware.
- Test conditional requests with an `If-None-Match` header that matches the response `ETag`.
