Expand description
ETag middleware — hashes 2xx response bodies, returns 304 when
If-None-Match matches. See etag::EtagLayer.
ETag middleware — hashes response bodies and serves 304 Not Modified
when the client’s If-None-Match matches.
§Quick start
ⓘ
use rustango::etag::{EtagLayer, EtagRouterExt};
let app = Router::new()
.route("/api/posts", get(list_posts))
.etag(EtagLayer::default());§How it works
For every successful (2xx) response with a non-empty body:
- Compute a hash of the body bytes (64-bit FNV-1a + length)
- Set
ETag: "<base64 hash>"on the response - If the request’s
If-None-Matchmatches — a comma-separated list of etags, or the wildcard*— reply304 Not Modifiedwith an empty body, carrying the caching headers (Cache-Control,Vary,Expires, …) a matching 200 would have sent (RFC 7232 §4.1).
Non-2xx responses are passed through untouched.
§When to use
- Read-heavy GET endpoints whose responses repeat across requests
- Skip for personalized responses unless you scope by user in the cache key
- Skip for streaming/large responses (the middleware buffers the body)
Structs§
- Etag
Layer - ETag middleware configuration.
Traits§
- Etag
Router Ext - Extension trait —
.etag(layer)ergonomics on Router.