36

What happens if you don't set the cache-control header? Does HTTP specify a default behaviour that clients should follow or is it up to the clients to decide?

In Chrome, I have seen some Javascript requests that have no cache-control headers showing up as 200 (from disk cache). Is Chrome optimizing this?

In IE, I have seen it show up as a 304 (not modified).

How do I understand this better?

3
  • 1
    "no cache headers" meaning no Cache-Control header, or really no cache headers? Cache-control is not the only header that affects caching. Most web servers will send Last Modified headers for static assets like Javascript files. Commented Dec 8, 2017 at 13:29
  • "In Chrome, I have seen some Javascript requests that have no cache-control headers showing up as 200 (from disk cache). Is Chrome optimizing this?" Yes, chrome is using a heuristic, as described in some of the answers. Try this: touch a file (i.e. set its timestamp to now), "Clear Cache and Hard Reload", then "Normal Reload" repeatedly. You'll see "304 Not Modified" the first few times. Then a "200 (from disk cache)" will appear, and they will alternate between 200's and 304's, with the 304s becoming less frequent and the "200 (from disk cache)" becoming more frequent, as the cache ages.
    – Don Hatch
    Commented Sep 16 at 21:17
  • For the previously described experiment, don't use the main foo.html file of the site-- I don't think I've ever seen a "200 (from disk cache)" for that, since chrome apparently always consults the server for that, resulting in "304 Not Modified". Instead, use one of the .js or image files referred to by foo.html; chrome apparently uses its heuristic for those.
    – Don Hatch
    Commented Sep 16 at 21:33

1 Answer 1

45

If there is no Cache-Control header and no Expires header, but there is a Last-Modified header (which most web servers send by default for static assets), most browsers will use heuristic freshness to determine how long to cache that asset for.

The typical calculation for this (which is the one suggested by RFC 7234) is:

(current time - last modified time) / 10

So, for example, if the last modified time for an image states it was last changed 60 days ago, a browser might decide it could reasonably use the locally cached version of that image for 6 days before checking for a new version.

Note that in most cases, if you hit refresh, you are telling the browser to fetch a fresh version, so in that situation this logic wouldn't apply.

Freshness as determined by actual browsers:

5
  • 11
    And if there is no Last-Modified header?
    – bertonc96
    Commented Apr 6, 2020 at 14:42
  • 1
    This is the answer I've been looking for. I am curious, however, what various browsers have as default.
    – aross
    Commented Nov 18, 2020 at 12:27
  • 4
    @bertonc96 It seems to be 0 in that case. See for example this.
    – aross
    Commented Nov 18, 2020 at 16:35
  • 1
    This is 7 years old now. Is this still valid? Having a hard time finding resources for this that are more recent than 2018
    – dougalg
    Commented Aug 23 at 1:18
  • 1
    This is still valid, at least gecko source code hasn't changed github.com/mozilla/gecko-dev/blob/master/netwerk/protocol/http/…
    – fin
    Commented Sep 29 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.