Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS browser cache issues

Tags:

Good morning, I have a web application in production environement. The users are using it every day, when I publish an update and a user comes back to the web application he views the old version of the web application. He needs to refresh the browser to load the new version. How can I solve this problem? I cannot tell hundreds of users to refresh the page every time I publish an update (3-4 times a week).

like image 220
CaTourist Avatar asked May 23 '15 09:05

CaTourist


People also ask

How do I clear angular cache?

Angular cache There is no command to clear the cache, just run rm -rf . angular/cache .

How do I stop index HTML from being cached?

You have to set the Cache-Control header to let the browsers know that they don't have to cache any content for that request. ( Pragma & Cache-Control is one and the same thing but from the different HTTP specification.

What is .angular cache?

A cache object used to store and retrieve data, primarily used by $templateRequest and the script directive to cache templates and other data. angular.


1 Answers

The classic cache problem. Unless told otherwise (by the webserver) the browser is going to cache assets based on the files name. So when it sees the same script/style/image with the same name it will 304 and serve the cached version. There are several ways around this, you can:

Configure the webserver to tell browsers not to cache the file. This is obviously inefficient as the user will have to download the same file time and time again.

Append a query string parameter to the filename such as my-angular-code.js?x=123, again inefficient.

Fingerprint your assets. This is neatly explained in Rails' Asset Pipeline documentation. There are several ways to achieve this, there are grunt tasks and gulp tasks also available for this. Essentially it is the process of changing the file name only when the contents changes, forcing the browser to consider it a new file.

like image 111
juco Avatar answered Sep 30 '22 18:09

juco