Angular PWA 在 Android 主屏幕应用程序和浏览器上强制重新加载

标签 angular service-worker progressive-web-apps angular-service-worker

我有一个 Angular PWA,可以检查新版本并重新加载新的应用程序代码。 它在任何桌面浏览器上都能完美运行,但是当我在 Android 设备或移动浏览器上使用 chrome 制作主屏幕快捷方式时,当有新版本时,它不会更新其代码。 我的 Android 设备中有 Chrome 73.0.3683.9

我用 rxjs 添加了一个计时器,每 3 分钟检查一次更新(仅用于调试),如果有则重新加载。

我在此处添加代码,希望有人可以帮助我理解为什么它无法在移动设备上运行。

ngsw-config.json

{
  "index": "/index.html",
  "assetGroups": [
    {
      "name": "app",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/favicon.ico",
          "/index.html",
          "/*.css",
          "/*.js"
        ]
      }
    },
    {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/assets/**"
        ]
      }
    }
  ]
}

更新.service.ts

@Injectable()
export class UpdatesService implements OnDestroy {

    private _unsb$ = new Subject();

    constructor(private _swUpdate: SwUpdate) {

        if (this._swUpdate.isEnabled) {

            // every 3 minutes
            timer(0, 1000 * 60 * 3)
                .pipe(takeUntil(this._unsb$))
                .subscribe(() => {
                    this._swUpdate.checkForUpdate()
                        .then(() => console.log('Finish checking for updates...'))
                })
        } else console.log('No service worker allowed');
    }

    CheckForUpdates() {
        this._swUpdate.available
            .pipe(takeUntil(this._unsb$))
            .subscribe(event => {
                this._swUpdate.activateUpdate()
                    .then(() => window.location.reload())
            });
    }

    ngOnDestroy() {
        this._unsb$.next();
        this._unsb$.complete();
    }
}

应用程序组件.ts

export class AppComponent implements OnInit {

    constructor(private _updates: UpdatesService) { }

    ngOnInit(): void {
      this._updates.CheckForUpdates();
    }
  }

最佳答案

好的,所以我设法了解问题所在,在手机中,服务在应用程序稳定之前开始通过计时器进行轮询,因此由于某种原因,它没有正确检查更新

 this._swUpdate.checkForUpdate()
                    .then(() => console.log('Finish checking for updates...'))

所以我添加了一个检查应用程序是否稳定的方法,然后开始轮询更改。

更新.service.ts

  @Injectable()
  export class UpdatesService implements OnDestroy {

   private _unsb$ = new Subject();

   constructor(private _swUpdate: SwUpdate, appRef: ApplicationRef) {
       console.log('%c Update service is running...', 'color: green; font-weight: bold;');

       if (this._swUpdate.isEnabled) {
           console.log('%c Service worker enabled', 'color: orange; font-weight: bold;');

        // Allow the app to stabilize first, before starting polling for updates.
           const appIsStable$ = appRef.isStable.pipe(first(isStable => isStable === true));
           const everySixHours$ = interval(1000 * 60 * 60 * 6).pipe(startWith(0))
           const everySixHoursOnceAppIsStable$ = concat(appIsStable$, everySixHours$);

           everySixHoursOnceAppIsStable$
               .pipe(takeUntil(this._unsb$))
               .subscribe(() => {
                   console.log('%c Checks for updates...', 'color: blue; font-weight: bold;')
                   this._swUpdate.checkForUpdate()
                       .then(() => console.log('%c Finish checking for updates...', 'color: blue; font-weight: bold;'));
               });
       } else console.log('%c No service worker allow', 'color: red; font-weight: bold;');
   }

   SubscribeForUpdates(): void {
       this._swUpdate.available
           .pipe(takeUntil(this._unsb$))
           .subscribe(event => {
               console.log('current version is', event.current.hash);
               console.log('available version is', event.available.hash);
               this._swUpdate.activateUpdate()
                   .then(() => window.location.reload(true))
           });
   }

   ngOnDestroy(): void {
       this._unsb$.next();
       this._unsb$.complete();
   }
}

ngsw-config.json 和 app.component.ts 保持不变。 希望它对某人有帮助

关于Angular PWA 在 Android 主屏幕应用程序和浏览器上强制重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55881119/

相关文章:

node.js - cerf 在本地主机中从 ssl 生成 HTTPS 时出现问题

iOS PWA 特殊 mailto 链接在 pwa 浏览器中打开选项卡。并停留在空白标签

android - 如何在 android WebView 中使用服务 worker ?

angular - 在 Angular 5 应用程序中渲染 PDF

javascript - 如果未定义,则将值存储到indexedDB

angular - 轮询服务器的 PWA 永远不会更新到较新的版本

visual-studio - Blazor WebAssembly Visual Studio 调试器问题

angular - Compodoc 可以生成 Markdown 文档,而不是其默认的 HTML 文档吗?

javascript - 如何在 Angular 2 中使用 ngIf-else 中的表达式

angular - 上传和预览 GLTF 文件 (3D)