views:

6128

answers:

3

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.

+6  A: 

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.

Craig
Of course, that assumes the HTML isn't being cached too... ;)
RodeoClown
+3  A: 

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:

acrosman
+15  A: 

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>
aemkei
Yep, this is exactly what we do. Same thing for external CSS files.
dmercer
is the javascript for the svn_version=678 cached, or is it reloaded every time (cause theres an get parameter appended)?
Beerweasle