Understanding the “304 Not Modified” HTTP Status Code #

The “304 Not Modified” HTTP status code is a typical response from web servers when a client (such as a web browser) requests a resource that has not been modified since the last time the client requested it. This can be an effective way for web servers to save bandwidth and reduce server load, as the client can use the cached version of the resource rather than requesting it again.

When Is the “304 Not Modified” Status Code Used? #

The “304 Not Modified” status code is typically used with the Last-Modified header, which specifies the date and time the resource was last modified. When a client requests a resource, it can include an If-Modified-Since header, specifying the date and time that the client last received the resource. If the resource has not been modified since then, the server will send a “304 Not Modified” response along with the Last-Modified header, indicating that the client can use the cached version of the resource.

Here is an example of a request and response using the “304 Not Modified” status code:

Request:
GET /example.html HTTP/1.1
Host: www.example.com
If-Modified-Since: Sun, 06 Nov 1994 08:49:37 GMT

Response:
HTTP/1.1 304 Not Modified
Last-Modified: Sun, 06 Nov 1994 08:49:37 GMT

Benefits of Using the “304 Not Modified” Status Code #

There are several benefits to using the “304 Not Modified” status code:

  1. Reduced Bandwidth Usage: By only sending the “304 Not Modified” response and not the actual resource, web servers can save bandwidth and reduce server load. This is especially important for resources that are requested frequently, such as popular images or JavaScript files.

  2. Faster Page Load Times: By using the cached version of a resource, clients can avoid the delay of downloading the resource again, resulting in faster page load times.

  3. Improved User Experience: Faster page load times can lead to a better user experience, as users are less likely to become frustrated with slow-loading pages.

  4. Improved Google Crawl Efficiency: Google supports the 304 status code. If it receives the 304, independent from the request headers, it will just reused the content version they had “on file”, thus saving both ends, web server and Google, resources.

How to Implement the “304 Not Modified” Status Code #

There are a few steps to take in order to implement the “304 Not Modified” status code on your website:

  1. Set the Last-Modified header on your resources: The Last-Modified header should be set to the date and time that the resource was last modified. This can typically be done using the lastmod element in the <head> of your HTML pages, or by setting the Last-Modified header in your server-side code.
  2. Check the If-Modified-Since header in client requests: When a client makes a request for a resource, check the If-Modified-Since header to see if the resource has been modified since the last time it was requested. If it has not been modified, send a “304 Not Modified” response along with the Last-Modified header.
  3. Set the Cache-Control header: The Cache-Control header can be used to specify how long a resource should be cached by clients. For example, you can use the max-age directive to specify the maximum age of the resource, after which the client must request a new version of the resource. This can help to ensure that clients are using the most up-to-date version of your resources.
  4. Use server-side caching: Server-side caching can be used to store frequently requested resources in memory, reducing the need to retrieve them from slower storage systems such as disks. This can help to improve the performance of your website and reduce server load.

When Not to Use the “304 Not Modified” Status Code #

There are a few situations where we may not want to use the “304 Not Modified” status code:

  1. Resources That Change Frequently: If a resource is likely to change frequently, it may not be worth the effort to implement the “304 Not Modified” status code, as clients will need to request a new version of the resource on a regular basis.
  2. Resources That Are Not Cachable: Some resources, such as dynamic pages or pages with user-specific content, may not be suitable for caching. In these cases, it may be more appropriate to simply send the full resource in the response.
  3. Resources That Require Authentication: If a resource requires authentication, the “304 Not Modified” status code may not be appropriate, as the client may not have the necessary credentials to access the cached version of the resource.

Example Diagram of the “304 Not Modified” Process #

Here is a diagram showing the process of using the “304 Not Modified” status code:

sequenceDiagram Client ->> Server: GET /example.html Server -->> Client: HTTP/1.1 200 OK Client ->> Server: GET /example.html Server ->> Client: HTTP/1.1 304 Not Modified

As you can see, the client requests a resource, and the server responds with the complete resource.

On subsequent requests, the server responds with a “304 Not Modified” status code, indicating that the client can use the cached version of the resource.

We hope this article has helped you to understand the “304 Not Modified” HTTP status code and how it can be used to improve the performance of your website. By implementing the “304 Not Modified” status code and using server-side caching, you can reduce server load and improve page load times, resulting in a better user experience for your website visitors.

We'd be happy if you share this