S
S
Samat2021-09-09 19:13:06
Angular
Samat, 2021-09-09 19:13:06

How to clear user cache in angular app?

And so it is, a large application on angular 11
The assembly takes place by ng build --outputHashing=all
But this does not help, the browser cache is not updated for users, respectively, you yourself understand what this means, while it seems that even the chunks remain old
Statics gives nginx

How to solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Samat, 2021-09-13
@Mileon

In my case, it turned out that the problem was in the ServiceWorker and the solution was:

constructor(
    public updates: SwUpdate
  ) {
    if (updates.isEnabled) {
      interval(30 * 60 * 1000).subscribe(() =>
        updates.checkForUpdate().then(() => console.log('Проверка обновлений'))
      );
    }
    this.update();
  }

  update(): void {
    this.updates.available.subscribe((event) => this.promptUser(event));
  }

  promptUser(event: UpdateAvailableEvent): void {
    console.log(
      'Обновление до новой версии: ',
      event.available.hash.substr(0, 6)
    );
    console.log('Устаревшая версия: ', event.current.hash.substr(0, 6));
    this.updates.activateUpdate().then(() => document.location.reload());
  }

AngularApp checks every half hour (optionally, according to the angular documentation, they have such a check every 6 hours) for updates on the server and if there is a newer version, then the page will reload to the new version, you can notify the user about this by asking if he wants to update it now
More detailed here:
https://angular.io/guide/service-worker-communications

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question