Last-Modified Header
You'll learn how to implement time-based cache validation using the Last-Modified and If-Modified-Since headers.
What Is the Last-Modified Header?
The Last-Modified header is a response header that tells the browser when a resource was last changed. It works hand-in-hand with If-Modified-Since to enable conditional requests—a way for browsers to ask "has this resource changed since I last downloaded it?"
Think of it like asking a librarian if a book has been updated since you last borrowed it. If it hasn't changed, they don't hand you the whole book again—they just say "nope, yours is still current."
How Time-Based Validation Works
- First request: Server responds with the resource and includes a
Last-Modifiedheader with a timestamp - Browser caches the resource along with that timestamp
- Subsequent request: Browser sends an
If-Modified-Sinceheader with the stored timestamp - Server checks: If the resource hasn't changed since that date, it returns
304 Not Modifiedwith no body - Browser uses cache: The cached version is still valid
Example Flow
// First response from server
HTTP/1.1 200 OK
Last-Modified: Wed, 15 Mar 2024 10:30:00 GMT
Content-Type: text/css
/* CSS content here */
// Browser's next request
GET /styles.css HTTP/1.1
If-Modified-Since: Wed, 15 Mar 2024 10:30:00 GMT
// Server response if unchanged
HTTP/1.1 304 Not Modified
No body is sent with the 304 response—saving bandwidth and time! The browser simply uses its cached version.
Key Takeaway: The Last-Modified header enables time-based cache validation, allowing servers to return lightweight 304 Not Modified responses when resources haven't changed, dramatically reducing bandwidth and improving performance.