304 Not Modified
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 header.
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 - 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 Not Modified header is returned to the client.
Both options mentioned above are suitable ways to determine whether or not the server should return a 304 not modified status code. The if modified since header is often used by CDNs to more efficiently deliver static content such as CSS, JS 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 304 not modified 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.