Skip to content
Velloreth
Web Development 5 min read

ETags Explained: Conditional Requests and 304s

An ETag is a fingerprint of a response. Learn how a conditional request works, the weak and strong forms, and the common mistakes.

By

What an ETag is

An ETag is an opaque identifier for a specific version of a resource.

HTTP/1.1 200 OK
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
Content-Type: text/html

The server sends it, and the client stores it with the response.

On the next request, the client echoes it:

GET /page HTTP/1.1
If-None-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4"

If the content is unchanged, the server returns:

HTTP/1.1 304 Not Modified
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"

A 304 has no body, so the revalidation costs a round trip and a few headers, not the full response.

How it is generated

A hash of the content is the correct approach.

const etag = `"${createHash('sha1').update(body).digest('hex')}"`;

A version number from the database works too, as long as it changes when the content changes.

A file’s modification time and size is a common shortcut for a static file.

The requirement: the ETag must change when the content changes, and must not change when it does not.

Strong versus weak

A strong ETag means the two representations are byte-identical.

ETag: "abc123"

A weak ETag means they are semantically equivalent but may differ in a way that does not matter.

ETag: W/"abc123"

A weak ETag is appropriate when the content is equivalent but not identical, such as a response that differs only in whitespace or in a header.

A range request requires a strong ETag, because the client is assembling a byte range and needs the bytes to match.

ETag versus Last-Modified

Last-Modified: Wed, 21 Oct 2026 07:28:00 GMT

The client sends If-Modified-Since, and the server returns a 304 if the resource has not changed since that time.

The limitation: the resolution is one second, so two changes within a second are indistinguishable.

An ETag is more precise, because it is a content fingerprint.

A server can send both, and the client sends both conditional headers. The server uses the ETag if it is present.

The revalidation flow

Client                          Server
  |                               |
  |-- GET /page ----------------->|
  |<-- 200 + ETag: "abc" ---------|
  |                               |
  |  (cache expires)              |
  |                               |
  |-- GET /page ----------------->|
  |   If-None-Match: "abc"        |
  |<-- 304 Not Modified ----------|
  |                               |
  |  (uses the cached body)       |

The 304 saves the body, which is the largest part of the response.

The round trip still happens, so a max-age that avoids the request entirely is faster. The ETag is for the case where the browser must check.

Common mistakes

An ETag that changes on every request. The revalidation always returns a 200, so the cache is useless. This happens when the ETag is derived from a timestamp or a random value.

An ETag that does not change when the content does. The client keeps a stale copy, which is worse than no cache.

A proxy that modifies the response without updating the ETag, which breaks the fingerprint.

A weak ETag on a range request, which is not allowed.

No ETag and no Last-Modified. The browser must download the full response to check.

An ETag on a response with no-store. The directive says not to store it, so the ETag is never used.

ETags and a CDN

A CDN may strip or rewrite an ETag, because it compresses the response and the bytes differ.

A weak ETag survives the transformation, which is one reason a CDN uses one.

The CDN’s own cache is controlled by s-maxage and stale-while-revalidate, and the ETag is for the client revalidation.

A purge on deploy is the way to invalidate the CDN’s copy, rather than relying on the ETag.

When to use an ETag

A dynamic page that changes occasionally, where a revalidation is cheaper than a full response.

An API response that is large and changes rarely.

A static file served without a hashed filename.

Not for a hashed asset. The URL is unique per version, so immutable is the right directive and the ETag is redundant.

ETags in practice

The value of an ETag depends on the response, and it is easy to add one that does nothing. A dynamic page that changes a few times a day is the ideal case: the browser revalidates, the server compares the fingerprint, and most requests return a 304 with no body. A large API response that changes rarely is the second good case, because the body is the expensive part and the 304 avoids it entirely. A hashed static asset is the case where the ETag is redundant, because the URL is unique per version and the immutable directive means the browser never asks. The case to avoid is a response that is generated per request with a fingerprint that varies, because the revalidation always returns a full response and the cache provides no benefit while adding a round trip. Checking the actual behaviour in the network panel is the fastest way to confirm that the ETag is doing its job.

Practical guidance

  • Derive the ETag from the content.
  • Send both an ETag and a Last-Modified where possible.
  • Use a weak ETag when the content is equivalent but not identical.
  • Use a strong ETag for a range request.
  • Check that the ETag changes when the content changes.
  • Check that it does not change when the content does not.
  • Skip the ETag for a hashed asset.
  • Purge the CDN on deploy.

For the related guides, see http-caching-headers and how-cdns-actually-work. For the broader context, core-web-vitals-guide and caching-strategies-explained.

Quotable summary

An ETag is a fingerprint of a response, sent by the server and echoed by the client in an If-None-Match header, and a conditional request returns 304 Not Modified when the content is unchanged. A 304 has no body, so the revalidation is cheap and saves bandwidth, though the round trip still happens, which is why a max-age that avoids the request entirely is faster. A strong ETag means byte-identical and a weak ETag means semantically equivalent, and a range request requires a strong one. An ETag that changes on every request defeats the cache, which is a common misconfiguration caused by deriving it from a timestamp or a random value rather than from the content.

Frequently asked questions

What is an ETag?
An opaque identifier for a specific version of a resource, usually a hash of the content. The server sends it, and the client echoes it on the next request.
What is a conditional request?
A request with a header such as If-None-Match that asks the server to return the body only if the resource has changed. If it has not, the server returns 304 Not Modified.
What is the difference between a strong and a weak ETag?
A strong ETag means the two representations are byte-identical. A weak ETag, prefixed with W/, means they are semantically equivalent but may differ in a way that does not matter.
What is the difference between ETag and Last-Modified?
Last-Modified is a timestamp with a one-second resolution, so two changes within a second are indistinguishable. An ETag is a content fingerprint, which is more precise.
Why does my ETag change on every request?
The server is generating it from something that varies, such as a timestamp or a random value, or a proxy is modifying the response. The ETag must be derived from the content.
Does an ETag replace Cache-Control?
No. Cache-Control decides whether the browser uses the cached copy without asking. An ETag makes the revalidation cheap when the browser does ask.