4

Just wondering - does Angular have a way of guaranteeing that the cache is cleaned for a user when they visit a site?

I have an Angular app that I occasionally push changes up to, but unfortunately users can't see those updates unless they open it in incognito. How do I fix this?

yoursweater
  • 1,883
  • 3
  • 19
  • 41
  • 1
    The folks who own/run the angular-material project haven't even figured it out with their own demo site, so I'm not too hopeful there's a broadly supported solution. – Roddy of the Frozen Peas Sep 05 '18 at 19:11
  • What is the nature of the changes? Code changes, or data changes? – Robert Harvey Sep 05 '18 at 19:12
  • 1
    One solution is to configure your web server (ex apache) to never cache ```index.html```. Whenever a new build is deployed, append a version number to all the files (ex ```main-1.2.1.js```). I believe ```angular-cli``` automatically does this by hashing the file names. – Sumama Waheed Sep 05 '18 at 19:15
  • When you build to --prod the files should have appended hashes that **should** trigger a fresh request for the resource. So when you're looking at your debug window for Network, those files if the same should have an http 304 not modified, if they're new they should just have a 200 stating they got grabbed again fresh. – Chris W. Sep 05 '18 at 19:15

1 Answers1

1

For controlling specific files that may change time to time, you can add a fake version parameter to the url, and change the value of the parameter.

For example: <script src="http://wwww.xxxx.com/js/my-site-script.js?version=1"></script>

When the value of the parameter is changed, the cache for that resource will be busted.

For testing, what I find useful is in the inspector (chrome / firefox at least have it), in the network tab, there is a checkbox to disable caching. When it is checked and that tab is open, you can load your page and it will not be cached. Of course this is for you when developing, not for the users of the site.

For a generic way not to cache files look int this question to see how to disable caching in the web browser: How to control web page caching, across all browsers?

Juan
  • 5,525
  • 2
  • 15
  • 26