The Last-Modified
header is an HTTP response header that indicates the date and time at which the server believes the requested resource was last modified. This header is used by clients, such as web browsers, to facilitate caching and conditional requests, which can help reduce bandwidth usage and improve the overall performance of web applications.
Purpose
The primary purpose of the Last-Modified
header is to provide a timestamp for the requested resource’s last modification. This information can be used by clients to determine if their cached version of the resource is still up-to-date or if they need to request a new version from the server.
Additionally, the Last-Modified
header enables clients to make conditional requests, which only return the resource if it has been modified since the specified date and time. This can help reduce the amount of data transferred between the client and server, leading to faster load times and reduced server load.
Usage in HTTP Requests and Responses
In HTTP Responses
When a server returns a resource in response to an HTTP request, it may include the Last-Modified
header to indicate when the resource was last modified. The value of this header is a timestamp in the format specified by RFC 7231, which is the same format used by the Date
header.
Here’s an example of an HTTP response that includes the Last-Modified
header:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1024
Last-Modified: Mon, 22 Nov 2022 12:34:56 GMT
<!DOCTYPE html>
<html>
...
</html>
In this example, the server indicates that the requested resource was last modified on November 22, 2022, at 12:34:56 GMT.
In HTTP Requests
Clients can use the If-Modified-Since
header in their HTTP requests to make conditional requests based on the Last-Modified
header. If the requested resource has not been modified since the specified date and time, the server will return a 304 Not Modified
status code and omit the resource’s content.
Here’s an example of an HTTP request that includes the If-Modified-Since
header:
GET /index.html HTTP/1.1
Host: example.com
If-Modified-Since: Mon, 22 Nov 2022 12:34:56 GMT
In this example, the client is requesting the /index.html
resource, but only if it has been modified since November 22, 2022, at 12:34:56 GMT.
If the resource has not been modified since the specified date and time, the server will return a response like this:
HTTP/1.1 304 Not Modified
Date: Tue, 23 Nov 2022 10:00:00 GMT
If the resource has been modified since the specified date and time, the server will return the updated resource along with a new Last-Modified
header:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 2048
Last-Modified: Tue, 23 Nov 2022 09:00:00 GMT
<!DOCTYPE html>
<html>
...
</html>
Summary
The Last-Modified
header is an essential component of HTTP that enables clients to cache resources and make conditional requests based on when a resource was last modified. By understanding how this header is used in both HTTP requests and responses, you can optimize your web applications to reduce bandwidth usage, improve performance, and provide a better user experience.