Koa ETag Middleware

5.0.0 · maintenance · verified Wed Apr 22

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.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up `koa-etag` in a Koa application, showing its typical usage alongside `koa-conditional-get` and `koa-compress`.

const Koa = require('koa');
const etag = require('koa-etag');
const conditional = require('koa-conditional-get');
const compress = require('koa-compress');

const app = new Koa();

// compress must be used before conditional and etag for correct ETag calculation
app.use(compress());

// etag works together with conditional-get for full HTTP caching support
app.use(conditional());
app.use(etag());

app.use(function (ctx) {
  ctx.body = 'Hello World';
});

app.listen(3000);

console.log('Server listening on port 3000');

view raw JSON →