3xx Redirects
Last updated Jul 21, 2023

304 - Not Modified

Benjamin Bouchet
Get your free
API key now
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

The HTTP Status 304 indicates that the requested resource has not been modified since the version specified by the request headers  If-Modified-Since or If-None-Match.

What is the HTTP Status Code 304?


In the world of web development, understanding the nuances of HTTP (Hypertext Transfer Protocol) response status codes is vital. Among these codes, the HTTP Status Code 304, commonly referred to as "Not Modified," holds a unique position. A part of the Redirection 3xx category in the HTTP status code specifications, this specific code plays a crucial role in optimizing web performance, improving load times, and reducing unnecessary network traffic.


When a client sends a request to the server, it can include a header field, typically the If-Modified-Since or If-None-Match. These fields convey the timestamp of the last modification or a unique identifier (ETag) for the version of the resource that the client has cached. If the server identifies that there's no change in the resource since the client's cached version, it responds with a 304 status code.


Unlike most HTTP status codes, a 304 response is expected not to include a message-body. This is because the response is essentially an instruction for the client to use its cached version of the requested resource, negating the need for sending new data.


However, while the HTTP Status Code 304 is grouped under the Redirection 3xx category, it behaves differently than its peers. Most 3xx codes instruct the client to look elsewhere or take additional action to complete the request. In contrast, the 304 status code allows the client to efficiently utilize its local cache instead of redirecting it to a different resource.


When and how is HTTP Status Code 304 used?


The HTTP Status Code 304 is typically used when a website is designed to exploit a browser cache’s capabilities to optimize its performance. Caching is a strategy where frequently accessed or static files (like CSS, JavaScript, and images) are stored in the client's browser after the initial load. Subsequent requests for the same resource, regardless of the search engine used, would then prompt the client to refer to its local cache, as opposed to re-fetching the browsing data from the server. This process significantly shortens page loading times.


Web servers employ the 304 status code to signal the client that its cached version of the requested resource remains valid and does not need to be re-downloaded. This decision is based on the request headers sent by the client, most commonly the If-Modified-Since or If-None-Match.


The If-Modified-Since header carries the date and time at which the client last received a copy of the resource. When the server receives a request with this header, it compares the provided timestamp with the resource's last modification date. If the server's version has not been modified since the given timestamp, it responds with a 304 status, indicating that the cached version is up-to-date.


On the other hand, the If-None-Match header uses a value known as an ETag, a unique identifier generated for each specific version of a resource. If the ETag provided in the If-None-Match header matches the ETag of the server's version, the server responds with a 304 status, instructing the client to use its cached copy.


Understanding the HTTP Status Code 304 and its use cases can significantly improve the performance of a web application by reducing unnecessary server load and network traffic, and taking full advantage of client-side caching. This status code, while relatively straightforward, is a crucial element of resource optimization and load-speed enhancements in modern web development.


Example usage of HTTP Status Code 304


To simplify the concept: imagine, for a moment, a library where every time you wished to revisit a book, you had to re-purchase and re-read it in its entirety, even if only a single page was added. Not a very efficient system, is it? This mirrors how a web browser fetches a webpage without HTTP Status Code 304 in play. Each time you visit, the browser fetches everything - including unchanged elements - from the server, which is a rather wasteful process.


Now, let's bring HTTP Status Code 304 into the picture. Using this status code, the server tells your browser to use its cached version of the webpage if nothing has changed since your last visit, saving significant time and network bandwidth. This is equivalent to adding only the new pages to your existing book rather than buying a new one each time - a far more effective approach.


To provide a technical perspective, let's simulate a simple GET request in JavaScript using the Fetch API. This request is aimed at fetching a CSS file stored at the path '/style.css':


fetch('/style.css', {
  headers: {
    'If-Modified-Since': 'Tue, 23 May 2023 00:00:00 GMT'
  }
})
.then(response => {
  if (response.status === 304) {
    console.log('Resource not modified. Use cached version.');
  } else {
    console.log('Resource modified. Fetching new version.');
  }
});


In the request above, the 'If-Modified-Since' header is included. This represents the timestamp of the version of the file in the client's cache. If the file hasn't been modified on the server since the date and time specified, the server will respond with the HTTP Status Code 304.


What is the history of HTTP Status Code 304?


HTTP Status Code 304 made its first appearance with HTTP/1.0, which was standardized in 1996. The principal reason behind its introduction was to augment web efficiency by reducing unnecessary network traffic, thereby ensuring more effective use of network bandwidth.

As web technology evolved, the role of HTTP Status Code 304 became more pronounced. With the advent of HTTP/1.1 and later HTTP/2, the complexity of web applications increased, leading to larger data transactions. This shift in web technology necessitated more efficient cache management, making the role of HTTP Status Code 304 critical in contemporary web development.

How does HTTP Status Code 304 relate to other status codes?

HTTP Status Code 304 is part of the 3xx category of HTTP status codes, also known as Redirection codes. Most 3xx codes guide the client to a different location to fulfill the request. However, the 304 status code instructs the client to utilize its local cache to meet the request, thereby improving performance and reducing bandwidth usage.


Comparatively, the 200-series codes signify successful completion of a request. A 200 (OK) status code, for instance, indicates that the server has successfully processed the client's request. If the server identifies a change in a resource, it will respond with a 200 status code alongside the updated resource. On the other hand, if the resource remains unchanged, the server will send a 304 (Not Modified) response, instructing the client to use its cached version.


In stark contrast, the 4xx and 5xx categories represent client and server errors, respectively. A 304 response, unlike these, does not represent an error but indicates a positive condition - that the cached copy remains valid and can be reused. Thus, understanding HTTP Status Code 304 in the context of other status codes provides a better picture of the entire HTTP response paradigm.


Other interesting facts about HTTP 304


After exploring the basic principles, usage scenarios, and historical context of the HTTP Status Code 304, let's delve a little deeper and uncover some additional intriguing facets of this particular status code.


One critical aspect to note is that HTTP 304 responses, by specification, should not contain a message body. The rationale behind this is quite straightforward: since the server instructs the client to use the cached version of a resource, there's no need to include any additional data in the response. However, it's still possible for a 304 response to contain specific header fields that might provide further directives or information.


In addition, HTTP 304 plays an important role in facilitating 'Conditional GET' requests. These requests use headers like 'If-Modified-Since' or 'If-None-Match' to ask the server to send the requested resource only if it has been modified since the last request. If not, the server replies with an HTTP 304 status code. This strategy is essential in maintaining cache freshness while ensuring efficient use of bandwidth.


Interestingly, HTTP 304 is deeply intertwined with the ETag (Entity Tag) HTTP header. An ETag is a unique identifier assigned to a specific version of a resource. When a client makes a request with an 'If-None-Match' header containing the ETag of its cached resource, the server compares this ETag with the one assigned to its current version of the resource. If they match, the server sends a 304 response. This allows for a higher level of cache control precision as it takes into account changes that may not alter the last modified date but should still be considered modifications.


This examination of HTTP Status Code 304 showcases its crucial role in efficient web performance. Beyond being a simple status code, its implications for user experience and resource management are both fascinating and pivotal in the digital age.

Get your free
API
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required