Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does browser automatically clear cache of external JavaScript file?

I have a JavaScript resource that has the possibility of being edited at any time. Once it is edited I would want it to be propagated to the user's browser relatively quickly (like maybe 15 minutes or so), however, the frequency of this resource being editing is few and far between (maybe 2 a month).

I'd rather the resource to be cached in the browser, since it will be retrieved frequently, but I'd also like the cache to get reset on the browser at a semi-regular interval.

I know I can pass a no-cache header when I request for the resource, but I was wondering when the cache would automatically reset itself on the browser if I did not pass no-cache.

I imagine this would be independent for each browser, but I'm not sure.

I tried to Google this, but most of the hits I found were about clearing the browser's cache... which isn't what I'm looking for.

like image 418
Elijah Manor Avatar asked Oct 15 '08 22:10

Elijah Manor


People also ask

Does browser cache clear automatically?

Just like website servers, browsers cache most content on a page to shorten load times. So, the next time that user loads the page, most of the content is ready to go without needing to download additional data. Browsers also automatically clear cache until their cache is full or their “time to live,” or TTL, expires.

Can external JavaScript files be cached?

Yes it will cache it. So use timestamp or the updated version in the url via query string.

How long is JavaScript cached by browser?

If a user stops using the browser it is indefinitely. If he/she uses the browser rarely, it will be until the expiration - either by internal policy or by HTTP headers. If he/she uses the browser heavily, it can be 12 minutes or even less.

Do browser's cache JavaScript files?

In general, most modern browsers will cache JavaScript files. This is standard practice for modern browsers and ensures an optimized loading experience. Cached assets such as JavaScript will typically be served from the browser's cache instead of making another request for a resource that has already been retrieved.


2 Answers

You may pass a version string as a get parameter to the URL of your script tag. The parameter won't be evaluated by the static JavaScript file but force the browser to get the new version.

If you do not want to assign the version string every time you edited the source you may compute it based on the file system time stamp or your subversion commit number:

<script src="/script.js?time_stamp=1224147832156" type="text/javascript"></script> <script src="/script.js?svn_version=678" type="text/javascript"></script> 

   

like image 179
aemkei Avatar answered Sep 18 '22 15:09

aemkei


Put a version on your javascript code like this that is updated when you make a change

<script src="/code.js?ver=123" type="text/javascript"></script> 

They will then always get new version.

like image 45
Craig Avatar answered Sep 17 '22 15:09

Craig