Conditional Requests with ETag and If-None-Match
You'll learn how to reduce bandwidth and improve performance by using ETags to serve cached content when a resource hasn't changed.
The Core Idea
An ETag (entity tag) is a unique identifier the server sends in a response header that represents a specific version of a resource—like a fingerprint. When the client makes a future request for the same resource, it can send that ETag back using the If-None-Match header. If the resource hasn't changed (the ETag matches), the server responds with 304 Not Modified and no body, saving bandwidth. If it has changed, the server sends the full new resource with a 200 OK.
Why This Matters
Think of it like checking if your friend updated their profile picture. Instead of downloading the entire photo every time, you ask: "Is the picture still version XYZ123?" If yes, you use your cached copy. If no, you download the new one. This saves time and data transfer.
How It Works
First Request:
// Client makes initial request
fetch('/api/users/42');
// Server responds with ETag
// Status: 200 OK
// ETag: "abc123xyz"
// Body: { "name": "Alice", "email": "alice@example.com" }
Subsequent Request:
// Client includes If-None-Match with the stored ETag
fetch('/api/users/42', {
headers: {
'If-None-Match': '"abc123xyz"'
}
});
// If unchanged, server responds:
// Status: 304 Not Modified
// (No body sent—use your cached version!)
Your frontend can cache the response and reuse it whenever you get a 304, dramatically reducing payload sizes for frequently-accessed, rarely-changed resources.
Key Takeaway: ETags and If-None-Match enable efficient conditional requests—the server only sends full data when resources actually change, saving bandwidth and improving load times.