Skip to content

ETag 中间件

¥ETag Middleware

使用此中间件,你可以轻松添加 ETag 标头。

¥Using this middleware, you can add ETag headers easily.

导入

¥Import

ts
import { Hono } from 'hono'
import { etag } from 'hono/etag'

用法

¥Usage

ts
const app = new Hono()

app.use('/etag/*', etag())
app.get('/etag/abc', (c) => {
  return c.text('Hono is cool')
})

保留的标头

¥The retained headers

304 响应必须包含在等效 200 OK 响应中发送的标头。默认标头为 Cache-Control、Content-Location、Date、ETag、Expires 和 Vary。

¥The 304 Response must include the headers that would have been sent in an equivalent 200 OK response. The default headers are Cache-Control, Content-Location, Date, ETag, Expires, and Vary.

如果要添加发送的标头,可以使用 retainedHeaders 选项和包含默认标头的 RETAINED_304_HEADERS 字符串数组变量:

¥If you want to add the header that is sent, you can use retainedHeaders option and RETAINED_304_HEADERS strings array variable that includes the default headers:

ts
import { etag, RETAINED_304_HEADERS } from 'hono/etag'

// ...

app.use(
  '/etag/*',
  etag({
    retainedHeaders: ['x-message', ...RETAINED_304_HEADERS],
  })
)

选项

¥Options

optional strong:boolean

定义使用或不使用 弱验证。如果设置了 true,则将 w/ 添加到值的前缀。默认为 false

¥Define using or not using a weak validation. If true is set, then w/ is added to the prefix of the value. The default is false.

<徽章类型="info" 文本="optional" /> 保留标头:string[]

¥optional retainedHeaders: string[]

你想要在 304 响应中保留的标头。

¥The headers that you want to retain in the 304 Response.

optional 生成摘要:(body: Uint8Array) => ArrayBuffer | Promise<ArrayBuffer>

¥optional generateDigest: (body: Uint8Array) => ArrayBuffer | Promise<ArrayBuffer>

自定义摘要生成函数。默认情况下,它使用 SHA-1。此函数以响应主体作为 Uint8Array 调用,并应返回哈希作为 ArrayBuffer 或 Promise 1。

¥A custom digest generation function. By default, it uses SHA-1. This function is called with the response body as a Uint8Array and should return a hash as an ArrayBuffer or a Promise of one.

Hono v4.7 中文网 - 粤ICP备13048890号