Skip to main content

Module etag

Module etag 

Source
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:

  1. Compute a hash of the body bytes (64-bit FNV-1a + length)
  2. Set ETag: "<base64 hash>" on the response
  3. If the request’s If-None-Match matches — a comma-separated list of etags, or the wildcard * — reply 304 Not Modified with 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§

EtagLayer
ETag middleware configuration.

Traits§

EtagRouterExt
Extension trait — .etag(layer) ergonomics on Router.