@fastify/etag

中文文档
6.2.2发布于 2026/11/06MITTypeScriptCommonJS构建来源已验证近 30 天下载 50.1万

Automatically generate etags for HTTP responses, for Fastify

原始使用文档
文档目录9 节
  1. Install
  2. Compatibility
  3. Example
  4. Plugin Options
  5. Use custom hash function
  6. Automatic 304 status codes
  7. Acknowledgments
  8. Benchmarks
  9. License

@fastify/etag

CI NPM version neostandard javascript style

A plugin for Fastify that automatically generates HTTP ETags according to RFC2616-sec13.

The plugin can optionally send a 304 status code when an ETag matches the if-none-match header.

Install

npm i @fastify/etag

Compatibility

Plugin versionFastify version
>=6.x^5.x
>=4.x <6.x^4.x
^3.x^3.x
>=1.x <3.x^2.x

Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin in the table above. See Fastify's LTS policy for more details.

Example

'use strict'

const Fastify = require('fastify')
const Etag = require('@fastify/etag')

const app = Fastify()
app.register(Etag)

app.get('/', async (req, reply) => {
  return { hello: 'world' }
})

app.get('/manual-etag', async (req, reply) => {
  // This will disable automatic ETag generation
  // It will still return a 304 if the ETag matches
  reply.header('etag', '"foobar"')
  return 'world'
})

app.listen(3000)

Plugin Options

  • algorithm: all hashing algorithms the Node.js crypto module supports. Default: 'sha1'. ('fnv1a' is deprecated; use hashFn: Etag.fnv1a instead.)
  • hashFn: function used to generate ETags (called with string or Buffer payload; return an unquoted hash string). When provided, algorithm option will be ignored. Default: undefined.
  • weak: generates weak ETags by default. Default: false.

Use custom hash function

'use strict'

const Fastify = require('fastify')
const Etag = require('@fastify/etag')

const app = Fastify()
app.register(Etag, {
  hashFn: Etag.fnv1a // Use fnv1a provided by @fastify/etag
})

app.get('/', async (req, reply) => {
  return { hello: 'world' }
})

app.get('/manual-etag', async (req, reply) => {
  // This will disable automatic ETag generation
  // It will still return a 304 if the ETag matches
  reply.header('etag', '"foobar"')
  return 'world'
})

app.listen(3000)

Automatic 304 status codes

By default, the plugin sends a 304 status code when the ETag is equal to the Etag specified by the if-none-match request header.

This is often the desired behavior, but can be disabled by setting replyWith304: false.

Acknowledgments

The fnv1a logic was forked from Travis Webb (@tjwebb)'s https://github.com/tjwebb/fnv-plus, the actual implementation is contributed to fnv-plus by @desudesutalk in https://github.com/tjwebb/fnv-plus/pull/9, and adapted by @SukkaW to support buffers.

Benchmarks

Generating an etag will always be slower than not generating an etag. The generation speed also depends on the payload size and type (buffer or string):

  • For very small payloads (< 2 kb), use 'fnv1a'
  • For buffers above 2 mb, use 'md5'
  • In all other scenarios, use 'sha1' (default)
  • YMMV, see this issue where other algorithms such as crc32 for small payloads and murmurhash3-wasm for big buffers have performed better than the mentioned recommendations
  • Any etag generation results in at least 10% less op/s (up to 50% less op/s for huge payloads)

License

Licensed under MIT.

版本与发布记录

@fastify/etag 共发布 17 个版本,首次发布于 2022/04/27;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.2.2latest2026/11/068 天前
6.2.12026/10/091 个月前
6.2.02026/07/163 个月前
6.1.02025/10/111.1 年前
6.0.32025/01/071.9 年前
6.0.22024/12/141.9 年前

其他发布通道:next → 6.0.0。

关于 @fastify/etag 的常见问题

@fastify/etag 是什么?

Automatically generate etags for HTTP responses, for Fastify

如何安装 @fastify/etag?

在项目目录运行 npm install @fastify/etag,或使用 pnpm add / yarn add / bun add。

@fastify/etag 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fastify/etag 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fastify/etag 可以商用吗?

@fastify/etag 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fastify/etag 的下载量有多少?

根据 npm 官方下载统计,@fastify/etag 在 2026/10/15 – 2026/11/13 这 30 天内被下载 501,422 次,日均约 16,714 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fastify/etag 还在维护吗?

最新版本 6.2.2 发布于 2026/11/06(8 天前);至今共发布 17 个版本,其中过去 12 个月发布了 3 个;首次发布于 2022/04/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。