304 Not Modified
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.
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.
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.
The workflow of using
If-None-Match is quite similar to
If-Modified-Since as can be seen by the diagram below.
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:
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-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.
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.