Chrome cache overriding angularjs disabling of cache

4.6k views Asked by At

I am facing a peculiar issue wrt caching. I am using chrome currently I have angularjs service as below

var url = '/api/acts/' + accountId + '/policy/?filter=NEW_POLICY';
                return $http({
                    method : 'GET',
                    url : url,
                    cache : false,
                    headers: {'disableErrorRedirect': true,'Cache-Control' : 'no-cache'}
                });

Here i have set all attributes of not using the cache and always making a fresh call. But I have seen that 3 out of 5 times it is picking the same value. After sometime it automatically refreshes. Server side there is not caching issue as I am not caching it. In Chrome Dev tools, there is an option called Disable Cache and when i check it , the results are never cached and I get the latest results. I want to make sure that always refereshed data should be return despite of any settings on chrome. I am really confused how to handle this. As mentioned above, I have written all the angularjs and http code to avoid caching. Any help would be appreciated.

2

There are 2 answers

0
Seth Moore On BEST ANSWER

I had this same issue and resolved it by declaring the cache config globally. It's not ideal, but better than having to add a random string.

angular.module('app', [])
  .config(function ($httpProvider) {
    $httpProvider.defaults.headers.common['Cache-Control'] = 'no-cache';
    $httpProvider.defaults.cache = false;
  });
2
Sir Neuman On

What is the file you are trying to get? At the end of your url it has /policy/?filter=NEW_POLICY but there is no actual file name before the question mark and after the slash. But anyway...

You can always ignore the cached version and get from the server by using /baseroot/id/postroot/file?random_string. so for example:

var rand_num = Math.random();
var url = '/api/acts/' + accountId + '/policy/filename?v=' + rand_num + '&filter=NEW_POLICY' 

or just var url = '/api/acts/' + accountId + '/policy/?v=' + rand_num + '&filter=NEW_POLICY' I guess. Like i said, I don't know of situations where you have no filename at the end of the url path.

Not exactly sure if that's a valid query string for the url in your case, but you can try and see if it works.