3

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?

9
  • 1
    Doesn't ng build --prod already append hash to your assets for that purpose? Commented Nov 8, 2017 at 18:36
  • I have this configuration here so I should be able to add like this? "styles": [ "assets/styles/app.scss?ver=123", "assets/styles/fonts.scss?ver=123" ] Commented Nov 8, 2017 at 18:43
  • That's not how you do it with angular cli. When you ng build --prod the 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. Commented Nov 8, 2017 at 18:45
  • 2
    For example, I also have a styles.scss file in my global styles. When I build in development mode, I get a styles.css file. With ng build --prod, webpack produces styles.7c4c51d1276ebd077169.bundle.css which is generated based on the file's contents, so you can cache it forever. Commented Nov 8, 2017 at 18:49
  • 1
    At the moment we are using ng build. I will change it to ng build --prod and see whether it will be able to hash the css/js links Commented Nov 8, 2017 at 21:36

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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>
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.