Angular Service Worker SwUpdate.available未触发

时间:2018-06-21 12:44:25

标签: angular caching apache2 angular6 pwa

我很难将Angles Service Worker集成到我的应用程序中。我遵循了指南,到目前为止,它仍然有效。我可以在主屏幕上创建快捷方式,然后启动我的应用程序。问题是我的应用程序无法更新。如果我更改按钮的名称,请构建应用程序并将其放置到服务器上,直到我按F5键为止,应用程序仍会显示旧版本(重新启动应用程序也无济于事)。

我试图将以下代码放入我的应用程序的ngOnInot中,但没有帮助

ngOnInit() {
if (this._SwUpdate.isEnabled) {

  setInterval( () => {
    this._SwUpdate.checkForUpdate().then(() => console.log('checking for updates'));
  }, this.updateInterval);

  this._SwUpdate.available.subscribe(() => {

    console.log('update found');

    this._SwUpdate.activateUpdate().then(() => {
      console.log('updated');
      window.location.reload();
    });

  });

}

}

该应用程序正在我的apache2 linux计算机上运行。我的apache缓存了什么,或者为什么我的应用程序没有意识到有新版本?

在此先感谢您的帮助:)

编辑:

我的ngsw-config.json

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

编辑2:

如果我使用“ http-server”在本地运行应用程序,则可以使用,但是当我将文件复制到我的Apache时,它不会检测到更新。在网络选项卡中,我可以看到间隔有效,该应用每隔3秒从服务器获取一个新的“ ngsw.json”。如果我更新我的应用程序,则可以看到响应中包含“ ngsw.json”的新哈希值。之后,浏览器会从我的服务器加载新的“ index.html”和“ main。***。js”,但该应用程序不会应用新版本。根据我的代码,应该说“找到更新”,但是什么也没发生。

6 个答案:

答案 0 :(得分:8)

您可能需要告诉服务人员检查服务器的更新,我通常为此使用服务:

export class UpdateService {

  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.available.subscribe(event => this.promptUser());
  }

  private promptUser(): void {
    console.log('updating to new version');
    this.updates.activateUpdate().then(() => document.location.reload())); 
  }

在您的app-component.ts中:

  constructor(private sw: UpdateService) {
    // check the service worker for updates
    this.sw.checkForUpdates();
  }


无论出于何种原因,有时Angular都不会适当地重新注册服务工作者。因此,您可以修改main.ts

替换:

platformBrowserDynamic().bootstrapModule(AppModule);

使用:

platformBrowserDynamic().bootstrapModule(AppModule).then(() => {
  if ('serviceWorker' in navigator && environment.production) {
    navigator.serviceWorker.register('ngsw-worker.js');
  }
}).catch(err => console.log(err));

答案 1 :(得分:1)

这在mac / windows / ios / android所有设备上都对我有效

export class PwaUpdateService {

    updateSubscription;

    constructor(public updates: SwUpdate) {
    }

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

        if (this.updates.isEnabled) {
            // Required to enable updates on Windows and ios.
            this.updates.activateUpdate();

            interval(60 * 60 * 1000).subscribe(() => {
                this.updates.checkForUpdate().then(() => {
                    // console.log('checking 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.updates.activateUpdate().then(() => {
            window.location.reload();
        });
    }
}

答案 2 :(得分:0)

对应用程序进行更改,您需要使用此方法activateUpdate(

import { SwUpdate } from '@angular/service-worker';

export class AppComponent implements OnInit {
  constructor(private swUpdate: SwUpdate) { }
  ngOninit() {
    this.swUpdate.available.subscribe(event => {
      console.log('A newer version is now available. Refresh the page now to update the cache');
    });
    this.swUpdate.checkForUpdate()
  }
}

检查此链接:https://angular.io/guide/service-worker-getting-started

答案 3 :(得分:0)

在堆栈上的所有其他解决方案均无效后,我开始调试angular的ngsw-worker.js脚本。我已经跟踪了更新逻辑,并最终遇到了“ PrefetchAssetGroup”方法。每当方法被调用时,我都会插入日志记录,然后我意识到它一直在尝试缓存我的favicon.ico。 我已经在ngsw.json中检查了favicon.ico的位置,并意识到favicon不在此处。将图标放在该位置后,一切都可以正常工作。

答案 4 :(得分:0)

要检查是否有较新的Angular版本,您可以从任何组件调用以下方法,也可以仅在IsNewerVersionAvailable中复制app.component方法。

export class DataService {
    
    constructor(private http: HttpClient, private swUpdate: SwUpdate) { }

    private available: boolean = false;
 
    IsNewerVersionAvailable() {
        if (this.swUpdate.isEnabled) {
            this.swUpdate.available.subscribe(() => {
                this.available = true;
            });
            console.log(this.available);
        }
        return this.available;
    }    
}

答案 5 :(得分:0)

我在angular文档中发现了以下信息: dict.keys.contains() is actually O(1)

摘要: 在有角度的站点上有一个有用的端点可以显示服务工作者的状态:

/ngsw/state

Facebook Ad MediaView附加到您的站点地址。如果服务有问题,应在该处显示。