Ionic Angular PWA SwUpdate始终触发更新和重新加载

gpfsuwkq  于 2022-12-16  发布在  Ionic
关注(0)|答案(1)|浏览(195)

我有一个Angular离子PWA。每个软件包都是最新版本。
我有一个SwupdaterService和在生产模式下的应用程序总是重新加载,像循环,没有任何更新。
这里我的服务:

export class SwupdaterService {

  constructor(public updates: SwUpdate) {
    if (updates.isEnabled) {
      interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate()
        .then(() => console.log('checking for updates')));
    }
  }

  public checkForUpdates(): void {
    this.updates.versionUpdates.subscribe(event => this.promptUser());
  }

  private promptUser(): void {
    console.log('updating to new version');

    this.updates.activateUpdate().then((res) => { 
      alert('here!');
      document.location.reload()
    });
  }
}

在应用程序模块中,我有这个:

ServiceWorkerModule.register('ngsw-worker.js', {
        enabled: environment.production,
        // Register the ServiceWorker as soon as the application is stable
        // or after 30 seconds (whichever comes first).
        registrationStrategy: 'registerWhenStable:30000'
      }),

最后我的应用程序组件:

export class AppComponent {
  constructor(
    private platform: Platform,
    private sw: SwupdaterService,
    private localizationService: LocalizationService) { }

  async ngOnInit() {
    await this.initializeApp();
  }

  async initializeApp() {
    await this.platform.ready();
    this.sw.checkForUpdates();
    await this.localizationService.setInitialAppLanguage();
  }
}

有人知道为什么会这样吗?

sigwle7e

sigwle7e1#

我修复了这个问题,将checkForUpdates函数替换为这个函数:

public checkForUpdates(): void {
    this.updates.versionUpdates.
      pipe(filter((evt): evt is VersionReadyEvent => evt.type === 'VERSION_READY'),
        map(evt => ({
          type: 'UPDATE_AVAILABLE',
          current: evt.currentVersion,
          available: evt.latestVersion,
        }))).subscribe(evt => {
          this.promptUser();
        });
  }

就像我在服务中看到的。工人:

readonly versionUpdates: Observable<VersionEvent>;
    /**
     * Emits an `UpdateAvailableEvent` event whenever a new app version is available.
     *
     * @deprecated Use {@link versionUpdates} instead.
     *
     * The of behavior `available` can be rebuild by filtering for the `VersionReadyEvent`:
     * ```
     * import {filter, map} from 'rxjs/operators';
     * // ...
     * const updatesAvailable = swUpdate.versionUpdates.pipe(
     *   filter((evt): evt is VersionReadyEvent => evt.type === 'VERSION_READY'),
     *   map(evt => ({
     *     type: 'UPDATE_AVAILABLE',
     *     current: evt.currentVersion,
     *     available: evt.latestVersion,
     *   })));
     * ```
     */

相关问题