HTTP - Cache Validation (Conditional GET Request)

About

A conditional get request or cache validation request is a conditional request made by the user agent to see if a stale resource is still valid.

Example

If conditional headers leads to conditional requests. For instance:

If-Modified-Since: Sat, 07 Feb 2015 16:44:03 GMT
If-None-Match: "e026b5aa0e4be8d990f093176b3dcb7f"

Note that you can get one or both from the user agent (browser)

This conditional headers leads to conditional requests:

Headers Cache Validator Condition
If-Match ETag The Etag should be the same
If-None-Match ETag The Etag should not be the same
If-Modified-Since Last-Modified date The date should be more recent
If-Unmodified-Since Last-Modified date The date is older or the same
If-Range ETag or Last-Modified date Similar to If-Match, or If-Unmodified-Since for range request

The are known in the specification as Precondition Header Fields

Steps

The original fetch

The browser (or any other user-agent):

This header are called validator header because they will be send back to the server and will then permit to see if the resource has changed since the last fetch (hence validating the freshness of the cache).

sequenceDiagram participant Client participant Server Note left of Client: Initial Request
The cache is empty
and the request is not conditional Client->>Server: GET /doc/resource HTTP/1.1 Note right of Server: Resource Returned
One ore more validators are set Server->>Client: HTTP/1.1 200 OK
Last-Modified: date
Etag: "xyx"

The conditional request

When the resource becomes stale in the cache, the client may send a condition request to see if the resource is still valid. If this is the case and that the resource has not changed, the server sends back a 304 Not Modified response without the body making the request smaller in size.

The cache becomes fresh again, and the client uses the cached resource.

sequenceDiagram participant Client participant Server Note left of Client:
The cache is stal
A cache validation request
is send
with the conditional and
validator headers
Client->>Server: GET /doc/resource HTTP/1.1
If-Modified-Since
If-None-Match
Etag
Last-Modified Note right of Server: The server checks
if the resource has changed
with the validators
and returns a 304 or 200
alt if the resource has not changed (without body) Server->>Client: HTTP/1.1 304 OK
Last-Modified: date
Etag: "xyx" else if the resource has changed (with the body) Server->>Client: HTTP/1.1 200 OK
Last-Modified: date
Etag: "xyx" end

Documentation / Reference


Powered by ComboStrap