Course contentsShow
← Web Development
Lesson 2145 of 2,98239. Network PerformanceFree lesson

Last-Modified Header

Implementing time-based validation with Last-Modified and If-Modified-Since headers.

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

  1. First request: Server responds with the resource and includes a Last-Modified header with a timestamp
  2. Browser caches the resource along with that timestamp
  3. Subsequent request: Browser sends an If-Modified-Since header with the stored timestamp
  4. Server checks: If the resource hasn't changed since that date, it returns 304 Not Modified with no body
  5. 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.