HTTP 304 Not Modified

The 304 not modified response code is used to tell browsers that the content has not been updated, and should be loaded from the browser cache.

1841 views

Edited: 2019-11-23 06:28

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.

Tell us what you think:

  1. An in-dept look at the use of headings (h1-h6) and sections in HTML pages.
  2. Pagination can be a confusing thing to get right both practically and programmatically. I have put a lot of thought into this subject, and here I am giving you a few of the ideas I have been working with.
  3. The best way to deal with a trailing question mark is probably just to make it a bad request, because it is a very odd thing to find in a request URL.
  4. How to optimize image-loading and automatically include width and height attributes on img elements with PHP.
  5. HTTP headers are not case-sensitive, so we are free to convert them to all-lowercase in our applications.

More in: Web development