ETags for conditional requests and caching

14855
0

ETags enable efficient caching by allowing clients to make conditional requests that return 304 Not Modified when content hasn't changed. Rails automatically generates ETags based on response content, and fresh_when or stale? methods handle the conditional logic. When a client sends If-None-Match with a cached ETag, Rails compares it to the current response ETag and returns 304 with an empty body if they match. This saves bandwidth and server-side rendering time. ETags work particularly well for APIs serving relatively stable content like user profiles or configuration data. The challenge is ensuring ETag generation accounts for all dependencies—missing a dependency means clients receive stale data.