When does browser automatically clear cache of external JavaScript file?

CachingBrowser

Caching Problem Overview


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.

Caching Solutions


Solution 1 - Caching

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>

   

Solution 2 - Caching

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.

Solution 3 - Caching

HTTP provides several controls for caching that browsers ignore in different ways. If you set a reasonable expiration date, most browsers will check to see if they have the current version are appropriate frequencies.

The search term you want to include here (to avoid browser user instructions) is HTTP.

For more see:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElijah ManorView Question on Stackoverflow
Solution 1 - CachingaemkeiView Answer on Stackoverflow
Solution 2 - CachingCraigView Answer on Stackoverflow
Solution 3 - CachingacrosmanView Answer on Stackoverflow