We're currently working on a new project with regular updates that's being used daily by one of our clients. This project is being developed using angular 2 and we're facing cache issues, that is our clients are not seeing the latest changes on their machines.
Mainly the html/css files for the js files seem to get updated properly without giving much trouble.
You can control client cache with HTTP headers. This works in any web framework.
You can set the directives these headers to have fine grained control over how and when to enable|disable cache:
Cache-Control
Surrogate-Control
Expires
ETag
(very good one)Pragma
(if you want to support old browsers)Good caching is good, but very complex, in all computer systems. Take a look at https://helmetjs.github.io/docs/nocache/#the-headers for more information.
A combination of @Jack's answer and @ranierbit's answer should do the trick.
Set the ng build flag for --output-hashing so:
Then add this class either in a service or in your app.moudle
Then add this to your providers in you app.module:
This should prevent caching issues on live sites for client machines
angular-cli resolves this brilliantly by providing an
--output-hashing
flag for the build command. Example usage:Bundling & Tree-Shaking provides some details and context. Running
ng help build
, documents the flag:Although this is only applicable to users of angular-cli, it works brilliantly and doesn't require any code changes or additional tooling.
In each html template I just add the following meta tags at the top:
In my understanding each template is free standing therefore it does not inherit meta no caching rules setup in the index.html file.
Found a way to do this, simply add a querystring to load your components, like so:
This should force the client to load the server's copy of the template instead of the browser's. If you would like it to refresh only after a certain period of time you could use this ISOString instead:
And substring some characters so that it will only change after an hour for example:
Hope this helps
I had similar issue with the index.html being cached by the browser or more tricky by middle cdn/proxies (F5 will not help you).
I looked for a solution which verifies 100% that the client has the latest index.html version, luckily I found this solution by Henrik Peinar:
https://blog.nodeswat.com/automagic-reload-for-clients-after-deploy-with-angular-4-8440c9fdd96c
The solution solve also the case where the client stays with the browser open for days, the client checks for updates on intervals and reload if newer version deployd.
The solution is a bit tricky but works like a charm:
ng cli -- prod
produces hashed files with one of them called main.[hash].jsSince Henrik Peinar solution was for angular 4, there were minor changes, I place also the fixed scripts here:
VersionCheckService :
change to main AppComponent:
The post-build script that makes the magic, post-build.js:
simply place the script in (new) build folder run the script using
node ./build/post-build.js
after building dist folder usingng build --prod