Stephen Oberauer
Stephen Oberauer

Reputation: 5385

In Angular (2+) how do I force the client browser to re-download

I'm using Angular CLI to create an app.

I'd like the client browser to re-download whenever I release a new version of my app.

I assume I could do something like append ?v=1 to the URL of one of the JavaScript files in a script tag, but these tags are automatically added when I build.

Upvotes: 7

Views: 4552

Answers (1)

KrystianC
KrystianC

Reputation: 442

When you use "ng build --prod" or "ng build --output-hashing=all" command angular CLI will build your app (usually in 'dist' folder) with new hashed filenames. Users browser will recognize it as new files and download new version automatically.

EDIT:
I have done a quick test on one of my older project and see the results: ng build has test

I have changed only one line in ts file and two bundle.js hashed files changed. css and vendor stayed the same. So for the browser, it will mean that it's a brand new file. It is true that if browser cache the index.html with old references will still load an old one from a cache.

I saw it somewhere that people do popup prompt there is a new version available and encourage users to refresh a page. This I guess could be done with ajax call to API to ask for the latest version and compare it with the one stored in js file - just an idea.

Upvotes: 6

Related Questions