2

I know that there are a lot of posts about that in web, but none of them satisfied me. Most of people recommends appending a verizon number to each script/css file. But this sound like a lot of job to me (project is qute bit), maybe there is some way to version only output files, some CLI command I dont know?

My solution was based of simple pinging to API for new version number, when > than stored then reload the page. And here is a tricky part - even location.reload(true) does not refresh the cached angular stuff. Manually CTRL+SHIFT+R does this job nicely. This would be good enough for me if the programmaticly hard reload would work.

I also tried a this.compiler.clearCache(); but also with no effect.

Thanks in advance! :)

0

2 Answers 2

5

The @angular/cli does the cache busting for you. It appends the hash to script/css file names if you run the ng build with the --prod flag.

ng build

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

2 Comments

Thx! This would be perfect, but I m currently facing a problem "JavaScript heap out of memory" whe try build with --prod flag. I will try some workaround to increase the max RAM amount for build. Or maybe U know some?
Found a solution for max heap exception. Seems like I have to use a long command since we are using VSTS for auto deployment thus I cant just edit ng files. 'node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng build --prod ' Indeed I see a guid appended to files. Hope this will help me. Thx again!
2

EDIT: If you are using the angular-cli, the @Tomasz's answer is probably better than the methods listed below.

Ideally you would use a module bundler like Webpack or a task runner like Grunt to automatically 'fingerprint' your files (ie, append a unique identifier to the file name based on it's contents).

But if that is not an option, a simple hack would be to :

  1. set the caching headers on your HTML to avoid caching on the browser
  2. Append a random query parameter to your script tag URLs in your HTML which you can change each time you want a new version.

<script src='/js/some.js?v=1.1.3'></script>

Changing the random query parameter (v in the example) will ensure the browser makes a fresh request as the URL is different than the cached (older) version

Comments

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.