The 304 Not Modified Header is sent in order to tell the client that a resource has not been modified since the client downloaded it last.
Servers will typically respond with the last-modified header when a resource is cacheable. Clients will store a copy of this header, which is then delivered back to the server on subsequent requests. This is done in the if-modified-since request header.
Caching is useful to lower the bandwidth usage of both clients and servers, and may also allow servers to save other resources, especially when used on dynamic pages, since the server does not have to re-build the entire resource. Instead, a 304 response code is sent.
If caching is supported for a resource, typically a browser will save it in a local cache. Then, on the next request of the same resource, the browser will send a if-modified-since request header, asking the server if the resource has changed. This request header allows the server to check if the client's cached version of the resource is still fresh by comparing the if-modified-since with the current time stamp of the resource. If nothing has changed, a 304 Not Modified will be delivered, telling the browser to load the resource from the cache instead. The server then exits without sending a response body.
The below is a HTTP GET Request, as performed by a browser:.
GET /http-304-not-modified HTTP/1.1 Host: beamtic.com
The response can look like:
HTTP/1.1 304 Not Modified
When delivering a 304 response, no response body should be sent along with the headers.
Caching of Static Files
Caching of static files is typically controlled by the HTTP server automatically, while dynamic resources will need to be controlled programmatically.
If a static file has not been changed, the server usually responds with a HTTP 304 response code.
Caching of Dynamic Resources
In addition to the if-modified-since request header, the ETag header is sometimes used on dynamic resources in order to tell if the code or application itself has changed.
Changes to the underlying code will sometimes result in a modified resource, and it may desirable to force re-download of a resource when this is the case.
Another optimization that servers may employ, is server-side caching of dynamic resources. Some CMS systems will offer this as a build-in feature.