Heuristic Caching
You'll learn how browsers decide to cache resources even when the server doesn't tell them to.
What Is Heuristic Caching?
When a server sends a response without explicit cache directives like Cache-Control or Expires, browsers don't just give up—they make an educated guess about whether and how long to cache the resource. This is called heuristic caching.
Think of it like a librarian deciding how long to keep a book on the "recent returns" shelf. If the book was published years ago and hasn't been updated since, the librarian assumes it won't change soon and keeps it readily available longer than a newly released book that might have immediate revisions.
How Browsers Make the Decision
Most browsers follow the RFC 7234 specification, which suggests using 10% of the time since the resource was last modified as the cache duration. They calculate this using the Last-Modified header:
Freshness Lifetime = (Current Time - Last-Modified Time) × 0.1
For example, if a JavaScript file was last modified 100 days ago and has no Cache-Control header, the browser might cache it for approximately 10 days.
Why This Matters
Heuristic caching means you can't assume resources won't be cached just because you didn't set cache headers. This can cause confusion when you update a file on your server but users still see the old version. It's unpredictable across different browsers and can lead to inconsistent behavior.
The best practice? Always provide explicit cache directives. Don't leave caching decisions to browser heuristics—be intentional with your Cache-Control headers.
Key Takeaway: Browsers cache resources heuristically when no cache headers exist, typically based on the Last-Modified date, so always set explicit Cache-Control directives to maintain control over caching behavior.