HTTP status code 304 is an informational status code indicating that the client made a conditional request, and the server allows the request. However, the requested resource has not been modified since the last request. Therefore, the server does not return the content of the resource but informs the client that it can use the cached version.
Common Scenarios for 304 Status Code
-
Cache Validation: The client requests a resource and wants to determine whether it has been modified since the last request. If the resource has not changed, the server returns a 304 status code, allowing the client to continue using the cached version.
-
Reducing Bandwidth: By allowing the client to use locally cached resources, network bandwidth usage can be reduced since the server does not need to transmit the resource content.
-
Improving Efficiency: The client can use a faster local cached response rather than waiting for new resources from the server, which can enhance page load speed.
When the Server Returns a 304 Status Code
The server may return a 304 status code when the client’s request includes the following headers:
-
If-Modified-Since: The client informs the server to return the resource only if it has been modified since the specified date; otherwise, return a 304 status code.
-
If-None-Match: The client provides the ETag value of the resource, and if the resource has not been modified since that ETag, the server returns a 304 status code.
Response Headers
When returning a 304 status code, the server typically does not include a response body but may include the following headers:
- Date: The current date and time.
- Cache-Control: Caching directives for the resource.
- Expires: The expiration time of the resource.
- ETag: The entity tag for the resource, used for subsequent cache validation.
- Content-Location: The location of the resource.
Example Response
HTTP/1.1 304 Not Modified
Date: Wed, 21 Oct 2015 14:48:00 GMT
Cache-Control: max-age=3600
ETag: "123456789"
Content-Location: https://www.example.com/path/to/resource
In this example, the status code 304 indicates that the resource has not been modified since the client’s last request. The client can continue using its cached version without needing to re-download the resource.