-1

Any time I make changes to the code in my angularjs site, they do take effect until the user clicks the refresh button. Even if I log out to a page that is not angular then log back in it is the same old code until I click the refresh button. So if someone logs in it is the same as the last time they logged in regardless of changes and I obviously can't expect them to click refresh every time they log in.

So my question is how could I force a refresh of the code. I don't want to use window.reload or anything like that if there is a speacial angular way to accomplish this. I have tried clearing the template cache but it doesn't work. This is what I tried:

.run(function($rootScope, $templateCache) {
    $templateCache.removeAll();
    $rootScope.$on('$routeChangeStart', function(event, next, current) {
        if (typeof(current) !== 'undefined'){
            $templateCache.remove(current.templateUrl);
        }
    });
})
Brian
  • 903
  • 1
  • 7
  • 13
  • Sorry @Brian but this is an x/y question. I think that your question should be: "why are my changes not being updated unless I reload the page?" Because that's your real problem here. – Josep Oct 27 '14 at 13:38
  • why you dont using `$location.path("WHERE YOU WANT TO GO");` – Narek Mamikonyan Oct 27 '14 at 13:38
  • There happy? Now do you have an answer or you just wanted to correct my title? – Brian Oct 27 '14 at 13:45
  • @Narek I am not trying to change my location. And if I did it would still use the older code. – Brian Oct 27 '14 at 13:47
  • @Brian I wasn't asking you to change the title, but to re-factor your question. Forget about reloading the page, share the relevant code that causes your app not to be updated, try to make a [plunker](http://plnkr.co/edit) reproducing the issue, share what you have tried, etc. I'm sorry if you thought that my first comment was annoying, but I'm just trying to help you. Thanks! – Josep Oct 27 '14 at 13:56
  • It is not a specific part of my code. It is any code changes at all. Any code that I add does not get used if the user does not first refresh the page. It could be something as basic as adding an alert to a function. – Brian Oct 27 '14 at 13:59
  • 2
    @Brian oh, wait! What you mean is that when you make changes into the js files, since the js file is in the cache of the clients, they don't get the updated version of the js file unless they do a hard-refresh, right? Ok, ok, I get it now. I have a meeting right now, but I will answer your question after it... There are many ways to solve that. – Josep Oct 27 '14 at 14:02
  • Thank you! Yes exactly what I mean. Sorry for the confusion. – Brian Oct 27 '14 at 14:06
  • @Brian Sorry, I'm back from my meeting... Have you tried this: http://stackoverflow.com/questions/32414/how-can-i-force-clients-to-refresh-javascript-files – Josep Oct 27 '14 at 14:59
  • Yes I foind something similar here: http://stackoverflow.com/questions/118884/what-is-an-elegant-way-to-force-browsers-to-reload-cached-css-js-files and modified it a little. I figured it was an angular issue since I never had a problem with the browser caching js files prior to using it. – Brian Oct 27 '14 at 15:05

1 Answers1

1

My problem turn out to be the browser caching js files. I didn't think that would be the problem because prior to using angularjs I never had this problem. So to fix the issue I found this: How to force browser to reload cached CSS/JS files? and modified it a little so I didnt have to use all the url rewriting.

I added the php function

function get_version($file){
    return filemtime($_SERVER['DOCUMENT_ROOT'] . $file);
}

Then where I link my scripts I use:

<script src="/path/to/script.js?<?php echo get_version('/path/to/script.js');?>"></script>
Community
  • 1
  • 1
Brian
  • 903
  • 1
  • 7
  • 13