Clear browser cache in Angular Clear browser cache in Angular angular angular

Clear browser cache in Angular


When you are building the application using ng build, you should use the following flag:

--outputHashing=all

This is to enable cache-busting. This adds a hash to every single built file such that the browser is forced to load the latest version whenever you have uploaded a new version to your servers.

Threfore, one way to do it would be to run this:

ng build --prod --outputHashing=all

You may read more about the build options flags over here.

If you do not wish to append the flags when you run ng build, you should set it on your configurations at the angular.json file.

"configurations": {  "production": {    "optimization": true,    "outputHashing": "all",     .     .     .  }}


Try [window.]location.reload(true):

if (localStorage.getItem('refreshed') === null) {    localStorage['refreshed'] = true;    window.location.reload(true);} else {    localStorage.removeItem('refreshed');}

According to w3schools:

By default, the reload() method reloads the page from the cache, but you can force it to reload the page from the server by setting the forceGet parameter to true: location.reload(true).


I also update my app daily, I use this to notify the user that the app is out-dated and request for the page to be refreshed.

constructor(    private swUpdate: SwUpdate,){    public checkForUpdates(): void {    this.updateSubscription = this.swUpdate.available.subscribe(event => this.promptUser());    if (this.swUpdate.isEnabled) {      // Required to enable updates on Windows and ios.      this.swUpdate.activateUpdate();      interval(300000).subscribe(() => {        this.swUpdate.checkForUpdate().then(() => {         console.log('checked for updates');        });      });    }}    // Important: on Safari (ios) Heroku doesn't auto redirect links to their-    //https which allows the installation of the pwa like usual    // but it deactivates the swUpdate. So make sure to open your pwa on safari    //like so: https://example.com then (install/add to home)          }      promptUser(): void {        this.swUpdate.activateUpdate().then(() => {          const snack = this.snackbar.open('A New Update Is Available Click To           Reload', 'Reload');          snack            .onAction()            .subscribe(() => {              window.location.reload();            })          });      }