HTTP status code 304, ‘Not Modified’, is a response code indicating that the requested resource has not been modified since the last request. This status code is used to optimize the communication between the client and the server, reducing the need to transfer the same resource multiple times. When a client receives a 304 Not Modified status, it can use the cached version of the resource, saving bandwidth and improving performance.
Conditional Requests
To understand the 304 Not Modified status code, it’s essential to grasp the concept of conditional requests. A conditional request is an HTTP request that includes one or more conditional headers, such as If-Modified-Since
or If-None-Match
. These headers allow the server to determine whether the requested resource has changed since the client’s last request.
If-Modified-Since
The If-Modified-Since
header is used to request a resource only if it has been modified since the specified date. If the resource has not been modified since the specified date, the server returns a 304 Not Modified status.
Example Request:
GET /example-resource HTTP/1.1
Host: example.com
If-Modified-Since: Tue, 15 Nov 1994 12:45:26 GMT
If-None-Match
The If-None-Match
header is used to request a resource only if its ETag (entity tag) does not match any of the specified ETags. An ETag is a unique identifier assigned to a specific version of a resource. If the resource’s ETag matches one of the specified ETags, the server returns a 304 Not Modified status.
Example Request:
GET /example-resource HTTP/1.1
Host: example.com
If-None-Match: "1a2b3c4d"
Handling 304 Not Modified Responses
When the server returns a 304 Not Modified status, it will not include the requested resource in the response. Instead, the client should use its cached version of the resource.
Example Response:
HTTP/1.1 304 Not Modified
Date: Wed, 21 Oct 2015 07:28:00 GMT
ETag: "1a2b3c4d"
In this example, the server provides the ETag of the resource in the response header. The client can use this information to confirm that its cached version of the resource is still valid.
Summary
In summary, the HTTP status code 304 Not Modified is an essential optimization feature in HTTP communication. It allows clients to use cached resources when the server confirms that they have not been modified, reducing the need to transfer the same resource multiple times. By understanding and implementing conditional requests with headers like If-Modified-Since
and If-None-Match
, developers can improve the performance and efficiency of their web applications.