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 弱: 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.

optional 保留的头部: string[]

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

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

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

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

🌐 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 中文网 - 粤ICP备13048890号