ETag HTTP response header is an identifier for a specific version of a resource. It allows caches to be more efficient, and saves bandwidth, as a web server does not need to send a full response if the content has not changed. On the other side, if the content has changed, etags are useful to help prevent simultaneous updates of a resource from overwriting each other ("mid-air collisions").
If the resource at a given URL changes, a new
Etag value must be generated. Etags are therefore similar to fingerprints and might also be used for tracking purposes by some servers. A comparison of them allows to quickly determine whether two representations of a resource are the same, but they might also be set to persist indefinitely by a tracking server.
|Header type||Response header|
|Forbidden header name||no|
ETag: W/"<etag_value>" ETag: "<etag_value>"
'W/'(case-sensitive) indicates that a weak validator is used. Weak validators are easy to generate but are far less useful for comparisons. Strong validators are ideal for comparisons but can be very difficult to generate efficiently. Weak
Etagvalues of two representations of the same resources might be semantically equivalent, but not byte-for-byte identical.
- Entity tags uniquely representing the requested resources. They are a string of ASCII characters placed between double quotes (Like
"675af34563dc-tr34"). The method by which
ETagvalues are generated is not specified. Oftentimes, a hash of the content, a hash of the last modification timestamp, or just a revision number is used. For example, MDN uses a hash of hexadecimal digits of the wiki content.
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4" ETag: W/"0815"
Avoiding mid-air collisions
With the help of the
ETag and the
If-Match headers, you are able to detect mid-air edit collisions.
For example when editing MDN, the current wiki content is hashed and put into an
Etag in the response:
If the hashes don't match, it means that the document has been edited in-between and a
Precondition Failed error is thrown.
Caching of unchanged resources
Another typical use case of the
ETag header is to cache resources that are unchanged. If a user visits a given URL again (that has an
ETag set), and it is stale, that is too old to be considered usable, the client will send the value of its
ETag along in an
If-None-Match header field:
The server compares the client's
ETag (sent with
If-None-Match) with the
ETag for its current version of the resource and if both values match (that is, the resource has not changed), the server send back a
Not Modified status, without any body, which tells the client that the cached version of the response is still good to use (fresh).
|RFC 7232, section 2.3: ETag||Hypertext Transfer Protocol (HTTP/1.1): Conditional Requests|
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
|Feature||Android||Chrome for Android||Edge mobile||Firefox for Android||IE mobile||Opera Android||iOS Safari|