koa-etag is a Koa middleware package designed to provide ETag support for HTTP responses, leveraging the `etag` library to automatically generate ETag headers. This functionality is crucial for enhancing caching efficiency, as it allows clients (browsers, proxies) to conditionally request resources, thereby reducing bandwidth consumption and server load. The current stable version is 5.0.0, which notably features a complete rewrite in TypeScript, offering both ESM and CJS bundles, and requiring Node.js 18+. While there isn't a strict, frequent release cadence, major updates like v5.0.0 represent significant overhauls. Its primary differentiator is its seamless integration within the Koa ecosystem, typically working in conjunction with `koa-conditional-get` to enable full HTTP conditional GET support. However, it is important to note that the package's README strongly advises migrating to `@koa/etag` (v5+) as `koa-etag` is slated for deprecation in its next major release.
npm install koa-etagVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up `koa-etag` in a Koa application, showing its typical usage alongside `koa-conditional-get` and `koa-compress`.
Replace `npm install koa-etag` with `npm install @koa/etag` and update import paths accordingly. The API is largely identical.
Ensure `app.use(compress())` is called prior to `app.use(conditional())` and `app.use(etag())` in your Koa application setup.
Upgrade your Node.js runtime environment to version 18 or later to use `koa-etag` v5.x.
For CommonJS, use `const etag = require('koa-etag');`. For ESM, use `import etag from 'koa-etag';`. Avoid named imports like `import { etag } from 'koa-etag';`.For CommonJS, use `const etag = require('koa-etag');`. For ESM, use `import etag from 'koa-etag';` to correctly import the default middleware function.Ensure both `koa-conditional-get` and `koa-etag` are applied, and crucially, `app.use(compress())` must be called before `app.use(conditional())` and `app.use(etag())`.