How long does Google Chrome cache a resource if expires and/or no-cache headers are not set?
Asked Answered
I

2

70

We have been having a problem with Chrome caching a resource on our Glassfish server. The expires and no-cache headers are not being sent and the resource (an approximately 4 MB SWF file) is being cached by Chrome -- despite the presence of the Last-Modified header.

Sometimes Chrome will get a 304 code, and other times it simply does a 200 (from cache). I understand the 304 -- Chrome is likely checking the most recent Last-Modified date with the cached version to decide. But other times it does the 200 (from cache), which does not return any header information and appears that Chrome is simply assuming the file hasn't been modified instead of checking.

Google's own site states the following:

HTTP/S supports local caching of static resources by the browser. Some of the newest browsers (e.g. IE 7, Chrome) use a heuristic to decide how long to cache all resources that don't have explicit caching headers.

But this does not provide a definitive answer. Is this heuristic published anywhere? I realize there may not be a fixed answer (like 30 days), but some general guidelines would be useful. Furthermore, if Last-Modified is being set, I don't understand why Chrome isn't bothering to check that first.

Insignificancy answered 16/3, 2012 at 16:17 Comment(2)
It is published to the extent that Chrome's source is freely available.Infectious
hi - did you find any more info about how this heuristic is determined? If the logic is indeed based on DEFAULT_CACHE_TIME is there a Chrome URI where this setting is displayed?Tallou
G
26

The time the browser considers a cached response fresh is usually relative to when it was last modified:

Since origin servers do not always provide explicit expiration times, a cache MAY assign a heuristic expiration time when an explicit time is not specified, employing algorithms that use other header field values (such as the Last-Modified time)... If the response has a Last-Modified header field (Section 2.2 of [RFC7232]), caches are encouraged to use a heuristic expiration value that is no more than some fraction of the interval since that time. A typical setting of this fraction might be 10%. [https://www.rfc-editor.org/rfc/rfc7234#section-4.2.2]

The details of how Chrome (and other browsers) calculate that value, can be found in the source code (An example from Chrome v49). It would appear that Chrome also calculates the value relative to the Last-Modified header.

(Credit to this post)

Gummy answered 26/8, 2016 at 7:27 Comment(2)
What if there is no Last-Modified header?Chancellorsville
It'll depend on the browser I think. The code sample I noted from Chrome falls back on 'max_age_value' and Expires header, I think...Gummy
K
13

DEFAULT_CACHE_TIME = 300

I found the above by searching at http://code.google.com/p/chromium/source/search?q=DEFAULT_CACHE_TIME&origq=DEFAULT_CACHE_TIME&btnG=Search+Trunk for "DEFAULT_CACHE_TIME".

There's a file called "chromeextensionsdocs.py" that contains the DEFAULT_CACHE_TIME.

I believe this is seconds based on the example given at http://code.google.com/appengine/docs/python/memcache/overview.html
In "chromeextensionsdocs.py", the DEFAULT_CACHE_TIME is sent as the last param in memcache.add

I'm not totally sure if this is the right value or not but seems likely that it is when putting the pieces together.

Kline answered 16/3, 2012 at 16:48 Comment(4)
I don't have any special knowledge of this topic, other than reading some articles online. DEFAULT_CACHE_TIME might be the default for the memory cache, but allegedly Chrome has several caches, one of which is disk-based. I imagine a file will expire in the memory cache after 5 minutes, but it's potentially in the disk cache for much longer. This article provides insight into the way IE9 determines when cache policy-less files expire.Antependium
Unit of "300" is day ? hours ? seconds ?Dicentra
See answer - "I believe this is seconds based on..."Kline
I have seen resources with cache headers missing be cached in Chromes disk cache for weeks.Chancellorsville

© 2022 - 2024 — McMap. All rights reserved.