@usetoki/toki-etag
v0.1.0
Published
Automatic ETag validators and 304 Not Modified for dynamic toki responses.
Maintainers
Readme
@usetoki/toki-etag
Automatic ETag validators and 304 Not Modified for dynamic
toki responses. Static files are already validated
natively; this covers everything your handlers build.
npm install @usetoki/toki-etagUsage
import { createApp } from "@usetoki/toki";
import { etag } from "@usetoki/toki-etag";
const app = createApp();
etag(app);
app.get("/users/:id", (req) => db.user(req.params.id)); // now carries an ETagEvery GET/HEAD response gets an ETag derived from its body. When the client sends back
If-None-Match with that tag, toki answers 304 Not Modified with an empty body — the
handler still runs, but the payload doesn't go over the wire.
Options
weak— emit a weak validator (W/"…"). Defaultfalse.algorithm—"fnv1a"(default) is a fast, non-crypto hash that's plenty for a cache validator;"sha1","md5", and"sha256"usenode:cryptoif you'd rather.
A handler that sets its own ETag is left untouched, and streaming responses
(reply.stream) are skipped — they have no materialized body to hash.
