$ cat docs/middleware/etag.md
ETagETag
Save bandwidth on repeated reads: clients that already have the response get an empty 304.Economize banda em leituras repetidas: quem já tem a resposta recebe um 304 vazio.
What it doesO que faz
etag(options?: EtagOptions): MiddlewareHandler. For 200 responses to GET/HEAD it hashes the body (SHA-256, weak W/"..." by default) unless the handler already set an ETag, then answers 304 (with ETag, Cache-Control, Vary...) when If-None-Match matches, using weak comparison.
etag(options?: EtagOptions): MiddlewareHandler. Para respostas 200 de GET/HEAD ele faz o hash do corpo (SHA-256, fraco W/"..." por padrão) a menos que o handler já tenha definido um ETag, e responde 304 (com ETag, Cache-Control, Vary...) quando o If-None-Match casa, com comparação fraca.
Install and orderInstalar e ordenar
pnpm add @tupik/middleware Register it after compress in the list (so it runs inside it and hashes the uncompressed body) on read-heavy modules.
Registre depois do compress na lista (para rodar por dentro e fazer hash do corpo sem compressão) em módulos de muita leitura.
ExampleExemplo
import { createModule } from '@tupik/core';
import { etag } from '@tupik/middleware';
const catalog = createModule('/catalog')
.use(etag()) // W/"<sha256>" and 304 on If-None-Match
.get('/products', { access: 'public', handler: () => listProducts() });import { createModule } from '@tupik/core';
import { etag } from '@tupik/middleware';
const catalog = createModule('/catalog')
.use(etag()) // W/"<sha256>" e 304 no If-None-Match
.get('/products', { access: 'public', handler: () => listProducts() }); APIAPI
| OptionOpção | TypeTipo | Default / behaviorPadrão / comportamento |
|---|---|---|
weak | boolean | true. false emits strong "..." tags.true. false emite tags fortes "...". |
interface EtagOptions { weak?: boolean }
interface EtagOptions { weak?: boolean }
Errors and limitsErros e limites
- Only
200toGET/HEAD; other statuses and methods pass untouched. text/event-streamis skipped; any other body is buffered to be hashed.
- Só
200deGET/HEAD; outros status e métodos passam intactos. text/event-streamé ignorado; qualquer outro corpo é bufferizado para o hash.
When not to use itQuando não usar
For large or generated-on-the-fly bodies, set the ETag in the handler from a version column instead: hashing still runs the handler and reads the whole body.
Para corpos grandes ou gerados na hora, defina o ETag no handler a partir de uma coluna de versão: o hash ainda roda o handler e lê o corpo inteiro.