ETag and Last-Modified are validators. The browser uses them to ask "has this changed?" when a cached response is stale or marked no-cache. A 304 response means "no, reuse your copy".
How it works:
- The server sends an
ETag(an ID for this version of the content, often a hash), aLast-Modifieddate, or both. - When the browser needs to revalidate, it sends a conditional request. It includes
If-None-Match: <etag>orIf-Modified-Since: <date>. - If the content has not changed, the server replies 304 Not Modified with no body. The browser reuses its cached copy and marks it fresh again.
- If the content has changed, the server sends a normal 200 with the new content.
This still costs a round trip, but it saves the download.
ETag is more precise. Last-Modified has only one-second resolution, and it depends on file timestamps.
- The two validators are
ETagandLast-Modified. - The browser sends conditional requests with
If-None-MatchorIf-Modified-Since. - A 304 Not Modified response has no body, and the browser reuses its cached copy.
- Revalidation saves bandwidth, but not the round trip.
Read more