[ADP-370] HTTP Caching: Last-Modified
Procedures
- The server includes a
Last-Modifiedheader in the response, indicating when the resource was last changed. - The client stores this value and includes it in subsequent requests using the
If-Modified-Sinceheader. - The server compares the
If-Modified-Sincedate with the resource's last modification date. - If the resource hasn't been modified since the date in
If-Modified-Since, the server responds with a 304 Not Modified status.
Guidance
- SHOULD use
Last-Modifiedin the response header for caching when appropriate. - MUST ensure that the
Last-Modifieddate is accurate and updated whenever the resource changes. - SHOULD implement proper handling of
If-Modified-Sincerequests on the server-side. - MAY use
Last-Modifiedin conjunction withETagfor more robust caching mechanisms, as described in ADP-134.
Implementation Details
- The browser automatically attaches
If-Modified-Sincein subsequent requests. For non-browser clients (e.g., command-line tools likecurlor server-side requests), you may need to manually include theIf-Modified-Sinceheader. - Servers should implement a reliable method to track and update the
Last-Modifieddate. This could involve database timestamps, file system metadata, or other appropriate mechanisms. - When responding to an
If-Modified-Sincerequest:- If the resource has not been modified, return a 304 Not Modified status with no body.
- If the resource has been modified, return a 200 OK status with the full resource and an updated
Last-Modifiedheader.
Best Practices
- Use
Last-Modifiedfor resources that change infrequently and have a clear last modification time. - Combine
Last-ModifiedwithETagfor more precise caching control, as outlined in ADP-134. - Ensure your server's clock is synchronized to avoid issues with time-based caching.
- Be cautious with dynamically generated content; ensure the
Last-Modifieddate accurately reflects content changes. - Consider using
Cache-Controlheaders in addition toLast-Modifiedfor more granular cache control, as discussed in ADP-134.
Example
Server Response (Initial Request)
http
HTTP/1.1 200 OK
Content-Type: application/json
Last-Modified: Wed, 21 Oct 2023 07:28:00 GMT
Cache-Control: public, max-age=3600
{
"data": "This is the resource content"
}Subsequent Client Request
http
GET /resource HTTP/1.1
If-Modified-Since: Wed, 21 Oct 2023 07:28:00 GMTServer Response (Not Modified)
http
HTTP/1.1 304 Not ModifiedSecurity Considerations
- Ensure that
Last-Modifieddates do not reveal sensitive information about your system or update patterns. - Be aware that clients can manipulate the
If-Modified-Sinceheader; always validate it server-side. - Consider the implications of caching on sensitive or user-specific data.