Angular - is it ok to register ngsw-worker.js twice (in main.ts & app.module.ts)?

独自空忆成欢 提交于 2020-01-13 10:23:09

问题


I am registering ngsw-worker.js in both main.ts and app.module.ts. Is this ok?

Such as would it create 2 instances of service worker? Would there be a conflict in service worker?

main.ts

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

app.module.ts

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    ServiceWorkerModule.register('/ngsw-worker.js', {enabled: environment.production}),
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {
}

I am registering it in main.ts because of this reason.

It's registered in app.module.ts because default Angular-cli SW project adds it here.


After registering the service workers in both main.ts and app.module.ts, I add Firebase Cloud Messaging sdk to the Service Worker instances.

navigator.serviceWorker
          .ready
          .then((registration) => this.firebase.messaging().useServiceWorker(registration));

So in brief, registering two instances of ngsw-worker.js would this be a problem?

来源:https://stackoverflow.com/questions/51094037/angular-is-it-ok-to-register-ngsw-worker-js-twice-in-main-ts-app-module-ts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!