How Cache-Control: no-cache Works
Cache-Control: no-cache is an HTTP header directive that forces browsers and intermediate caches to validate the resource with the origin server before using a cached version. This doesn't mean the resource can't be cached; ratherit must be revalidated each time it is requested.
Let's go through an example of how this works in practice.
Initial Request
-
First Request by Browser:
The browser makes an initial request to the server to fetch a resourcefor examplea user profile picture.
GET /profile.jpg HTTP/1.1 Host: example.com -
Server Response:
The server responds with the resource and includes the
Cache-Control: no-cachedirective.HTTP/1.1 200 OK Content-Type: image/jpeg Content-Length: 12345 Cache-Control: no-cache ETag: "abc123"
Subsequent Requests
-
Second Request by Browser:
The browser caches the resource along with its headers. The next time the browser requests the same resourceit must first revalidate it with the server. This is indicated by including the
If-None-Matchheader with the ETag value in the request.GET /profile.jpg HTTP/1.1 Host: example.com If-None-Match: "abc123" -
Server Response (Resource Not Modified):
If the resource has not changedthe server responds with a
304 Not Modifiedstatusindicating that the cached version is still valid. The response does not include the resource bodysaving bandwidth.HTTP/1.1 304 Not Modified ETag: "abc123"The browser continues to use the cached version of the resource.
Handling Resource Changes
-
Server Response (Resource Modified):
If the resource has changedthe server responds with a
200 OKstatus and includes the new version of the resource along with a new ETag.HTTP/1.1 200 OK Content-Type: image/jpeg Content-Length: 12346 Cache-Control: no-cache ETag: "def456"The browser caches the new version of the resource and the new ETag value.
Example Workflow
Here’s a full example demonstrating the Cache-Control: no-cache workflow:
Initial Request and Response:
-
Initial Request:
GET /profile.jpg HTTP/1.1 Host: example.com -
Initial Response:
HTTP/1.1 200 OK Content-Type: image/jpeg Content-Length: 12345 Cache-Control: no-cache ETag: "abc123"
Subsequent Request and Response (Not Modified):
-
Subsequent Request:
GET /profile.jpg HTTP/1.1 Host: example.com If-None-Match: "abc123" -
Not Modified Response:
HTTP/1.1 304 Not Modified ETag: "abc123"
Subsequent Request and Response (Modified):
-
Subsequent Request:
GET /profile.jpg HTTP/1.1 Host: example.com If-None-Match: "abc123" -
Modified Response:
HTTP/1.1 200 OK Content-Type: image/jpeg Content-Length: 12346 Cache-Control: no-cache ETag: "def456"
Summary
Cache-Control: no-cache: Forces the browser to revalidate the resource with the origin server before using a cached version.- Revalidation: The browser includes the
If-None-Matchheader with the cached resource's ETag value. - Server Response: If the resource is unchangedthe server responds with
304 Not Modified. If the resource is changedthe server responds with the new version of the resource and updates the ETag.