304 Not Modified
Description
The 304 Not Modified
status code indicates that the resource has not been modified since the version specified by the request headers If-Modified-Since
or If-None-Match
. In such cases, there is no need to retransmit the resource since the client still has a previously-downloaded copy that is still valid.
This status code is used for conditional GET requests to reduce network bandwidth usage and server load. When a client has a cached version of a resource, it can include validation information in its request to ask the server if the cached version is still valid. If the resource hasn't changed, the server responds with a 304 Not Modified
status, and the client can use its cached copy.
The 304 Not Modified
response must not contain a message body, and is always terminated by the first empty line after the header fields.
Syntax
The server responds with a 304 Not Modified
status without a response body:
HTTP/1.1 304 Not Modified
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
Cache-Control: max-age=3600
Date: Wed, 15 Jun 2023 14:30:22 GMT
Note the absence of Content-Type and Content-Length headers, and the empty line at the end indicating no response body.
Examples
Browser Cache Validation Example
A client with a cached copy of a CSS file checks if it needs to download a new version:
Client Request:
GET /styles/main.css HTTP/1.1
Host: example.com
If-Modified-Since: Mon, 12 Jun 2023 09:45:22 GMT
If-None-Match: "a7f8d2e4c6b9"
Server Response (when file hasn't changed):
HTTP/1.1 304 Not Modified
ETag: "a7f8d2e4c6b9"
Cache-Control: max-age=86400
Last-Modified: Mon, 12 Jun 2023 09:45:22 GMT
Date: Wed, 15 Jun 2023 14:30:22 GMT
API Resource Caching Example
A client checks if its cached version of an API resource is still current:
Client Request:
GET /api/products/123 HTTP/1.1
Host: api.example.com
If-None-Match: "v1.2.5"
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Server Response (when resource hasn't changed):
HTTP/1.1 304 Not Modified
ETag: "v1.2.5"
Cache-Control: max-age=3600
Vary: Accept, Authorization
Date: Wed, 15 Jun 2023 15:45:12 GMT
Image Caching Example
A client checks if a cached image needs to be updated:
Client Request:
GET /images/logo.png HTTP/1.1
Host: assets.example.org
If-Modified-Since: Tue, 13 Jun 2023 10:15:30 GMT
Server Response (when image hasn't changed):
HTTP/1.1 304 Not Modified
Last-Modified: Tue, 13 Jun 2023 10:15:30 GMT
Cache-Control: public, max-age=86400
Expires: Thu, 16 Jun 2023 15:45:12 GMT
Date: Wed, 15 Jun 2023 15:45:12 GMT
Summary
The 304 Not Modified
status code is a key component of HTTP caching mechanisms, enabling significant bandwidth savings and improved performance for both clients and servers. By allowing clients to reuse their cached copies of resources that haven't changed, it reduces unnecessary data transfer and server processing. This status code is particularly valuable for static assets like images, CSS, and JavaScript files, as well as for API responses that don't change frequently. Proper implementation of conditional requests and 304 Not Modified
responses is an essential optimization technique for web applications and APIs.