Last-Modified Header
The Last-Modified header is an HTTP response header that indicates the date and time at which the resource was last modified on the server. It is used by clients (like web browsers) to determine whether they need to fetch a fresh copy of the resource or if they can use a cached version.
Here’s how it works:
- Client sends a request: When a client requests a resource from a server, it includes the If-Modified-Since header in the request, if it has a cached version of the resource. This header contains the timestamp of the last time the client fetched the resource.
- Server checks Last-Modified: The server compares the If-Modified-Since header in the request with the Last-Modified header of the resource.
- Server responds:
- If the Last-Modified header of the resource is later than the If-Modified-Since header in the request, it means the resource has been modified since the last time the client fetched it. The server sends a 200 OK response with the updated resource.
- If the Last-Modified header of the resource is the same or earlier than the If-Modified-Since header in the request, it means the resource has not been modified since the last time the client fetched it. The server sends a 304 Not Modified response, indicating that the client can use its cached copy of the resource.
Benefits of using Last-Modified header:
- Reduces network traffic: By using cached resources, clients can avoid downloading the same resource multiple times, reducing network load.
- Improves performance: Cached resources can load faster, improving the overall user experience.
- Saves server resources: By serving cached resources, servers can reduce the load on their resources.
Note:
- The Last-Modified header is a simple but effective mechanism for caching resources. However, it may not be suitable for all scenarios, especially when resources are frequently updated or when precise caching control is required. In such cases, other caching mechanisms like ETag or conditional GET requests may be more appropriate, and Last-Modified can be used as a fallback.