caching js files in ajaxOptions with renderPartial and processOutput true

So I noticed the jQuery option on ajax for caching basically just puts a time stamp at the end of the URL so it’s always unique to prevent browsers from caching the result. Actually setting caching to true doesn’t actually cache anything, it just doesn’t prevent it from being cached.

If I have an ajax request with something in CHtml and ajaxOption caching is false, it simply appends a GET variable "?_=3042349819" where the number is a time stamp.

If I set the ajaxOption caching to true, that variable isn’t there.

My issue is when there’s an ajax request that pulls some info that uses renderPartial with processOutput=true. It includes the js files, but even though in the request response there’s no time stamp on the actual script tag js src, when the browser requests it, the timestamp is included. It doesn’t matter if the ajax request has caching set to true or false, that only effects the request url.

So where are these time stamps coming from? It doesn’t seem like the browser would the timestamp there itself. But jQuery is only being told to insert an ajax response using .html().

If caching is set to true, then it shouldn’t include the timestamps on the js files either.


I can only guess that somehow .html() detects the script files and automatically calls $.getScript() on them. I couldn’t find anything specifically saying that happens though.

I did find that if I add:

 $.ajaxSetup({ cache:true });

Then when it loads the js files they don’t have the timestamp. So the only way to prevent caching:false is to set the global cache option to true. Maybe just before and after the request.

The browser is the one that caches data by default… that’s why there is the “cache:false” option to prevent browser caching…

As per jQuery.ajax() documentation

So maybe you use script or jsonp for datatype?

I’m not using any script or jsonp type dataTypes unless Yii is. I’m only using Yii’s ajaxLink and was observing its behavior. It seems that while that’s the default in the docs, it’s not for Yii. If not explicitly set, then Yii sets cache to false for all requests except scripts which default to false anyway. I suppose this is fine since if I set the global ajaxSetup cache to true, all the explicit requests will still be false.

I just don’t want it reloading jquery.js on every single ajax call!

EDIT: Actually, just found:

Yii::app()->clientScript->scriptMap['*.js'] = false;

while searching for how to fix a number of other issues (mainly undelegate not working with delegations set before it loads jquery again and thus causing many duplicate events)… Which is much better, heh.

You may try what I learnt here. ::)

Edit: Glad you found the solution.


Haha, yup! :P