304 Not Modified
Have you ever visited a website and noticed that it loads lightning-fast, even though you have a slow Internet connection? You might have encountered a server response code called 304 Not Modified
. In this article, we'll dive deep into what this HTTP status code means, how it works, and its importance for web performance optimization.
Understanding HTTP Status Codes
When you browse the Internet, your browser communicates with web servers using a protocol called Hypertext Transfer Protocol (HTTP). It is a set of rules for transferring files such as text, images, and videos over the Internet. During this communication, the server returns a status code to the browser to indicate the success or failure of the request. These status codes are three-digit numbers that fall into five categories: informational responses, successful responses, redirections, client errors, and server errors.
HTTP status codes are essential because they help the browser understand what's happening with the requested resource and take appropriate action based on that information. For example, a 200 OK
status code indicates that the requested resource is available and the server has successfully transferred it to the browser. In contrast, a 404 Not Found
status code indicates the requested resource is not available on the server.
Now that you have a basic understanding of HTTP status codes, let's explore the HTTP 304 status code and what it means for web performance.
What does 304 Not Modified
mean?
304 Not Modified
is an HTTP status code that is returned to the client when the cached copy of a particular file is up to date with the server. When a client such as a browser stores something in cache, it also keeps the Last-Modified
header sent from the server. This header provides the browser with the date and time of when the file was last modified.
When a request is made from the browser for the same file again, the If-Modified-Since
header is sent to the server. If this value and the server's ETag value for that file are both the same, the server sends back the 304 Not Modified
HTTP status.
Why is the HTTP 304 status code important for web performance optimization?
The HTTP 304 status code is essential for web performance optimization because it reduces network traffic and improves website loading speed. When a client requests a resource that it has already cached, the server can respond with a 304 Not Modified
status code instead of sending the same resource again.
This response reduces the amount of data transmitted over the network and speeds up website loading times, especially for users with slow internet connections.
Caching is a vital technique for web performance optimization. By caching frequently accessed resources, such as images, scripts, and stylesheets, servers can reduce the number of requests and data transfer required to load a webpage. Caching also improves website availability and reduces server load by serving cached resources instead of processing requests for the same resources repeatedly.
Using If-Modified-Since
vs If-None-Match
Using the If-Modified-Since
header is not the only way to determine whether the server should respond with a 304 Not Modified
. There is in fact, another header that can be used, which is the If-None-Match
header. Both of these options are further discussed in the section below.
If-Modified-Since
In order to visualize the flow of information from the browser to the server and back, consider the following workflow.
Once the server receives a request from a client, it checks to see if the If-Modified-Since
header is present. If no, the server will deliver the latest content to the client as this is the first time it is being requested. On the other hand, if the If-Modified-Since
header is present, the server will check to see if the file has been edited since the last time the browser accessed the file. If the answer is yes, the server returns a new copy to the client, if no, the server responds with a 304 Not Modified
and the browser can retrieve the content from its cache.
If-None-Match
The workflow of using If-None-Match
is quite similar to If-Modified-Since
as can be seen by the diagram below.
Using the If-None-Match
option to determine if a file has changed involves the creation of a unique identifier called an ETag. An ETag usually consists of a string of text and numbers generated by the server, resembling something similar to: flask-1448298997.74-7011-3318025237
. Once the client has requested a file, it will receive an ETag which the browser will use in conjunction with the If-None-Match
header for subsequent requests, similar to: If-None-Match: "flask-1448298997.74-7011-3318025237"
.
Since the server generates a new and maintains the previous ETag value each time the file is changed, it will check to see if the browser's ETag matches its own. If not, the server sends a new copy of the file and if yes, the 304 header is returned to the client.
Summary
Both options mentioned above are suitable ways to determine whether or not the server should return a HTTP 304 status code. The If-Modified-Since
header is often used by CDNs to efficiently deliver static content such as CSS, JavaScript, or images. Using the If-None-Match
header in conjunction with ETags can serve more useful when the content is dynamic and we would like to keep it in temporary memory.
The HTTP 304 status code is a fairly simple concept that harnesses the ability to save both resources and time. Each time a file must be delivered directly from the origin server, this means that the user must wait for the file to download which in turn uses bandwidth on the server's end. With the use of the 304 Not Modified
header, browsers and servers are able to efficiently communicate what needs to be downloaded again and what can be delivered from cache.