I would like to add versioning for my angular application to avoid cache like style.css?ver=1 ? Where should I add that in angular-cli.json?
1 Answer
Use ng build --prod, it will hash your assets and modules and append the hash to the file name.
You have nothing to manage manually.
2 Comments
Nitul
How can we add version number in URL for css and js? Can we automate it for all html files in build process? <script src="/js/app.<version-no>.js"></script>
msanford
@Nitul Like I said, using
ng build --prod does all of that for you (provided that the assets are part of your angular application). See my comments on the original post. Using "version numbers" is an antiquated idea that was only useful when it was managed by human brains who needed to remember what was going on. A hash is better, and is completely automatic.
ng build --prodalready append hash to your assets for that purpose?"styles": [ "assets/styles/app.scss?ver=123", "assets/styles/fonts.scss?ver=123" ]ng build --prodthe tool will compile your assets with a hash of each asset and then append it to the filename for you, updating the imports. You don't have to do anything to manage this yourself.styles.scssfile in my global styles. When I build in development mode, I get astyles.cssfile. Withng build --prod, webpack producesstyles.7c4c51d1276ebd077169.bundle.csswhich is generated based on the file's contents, so you can cache it forever.ng build. I will change it to ng build --prod and see whether it will be able to hash the css/js links