Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 app doesn't register service worker

I've an app that I already had built and SW were working fine. I continue to develop without paying attention to SW ... And now, there is no more SW getting download while charging the app (as if the app was not registring the SW).

I'm pretty desesperate as I have no idea on how to debug/found clue on that issue. How can I track the SW registring process ?

More context:

  • I have:
    • "serviceWorker": true in .angular-cli.json
    • ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production }) in my app.module.ts
    • build my app in prod mod and served it with http-server (both ngsw.json and ngsw-worker.js are accessible)
    • clean cache in dev mode (regular cache and application/clear storage)
  • I tried to:
    • change app.module imports order (i tried ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production }) first, second after BrowserModule and last)
    • validate that environment.production was true (it is)
    • generate an other empty app from scratch doing the same process (in that case SW works...)

Can i check something else? Did I forget something?

If not my two last solutions are:

  • moving module by module in the empty app to solved the problem without understanding the error
  • lighting fire + shamanic songs...
like image 849
Alexandre SIRKO Avatar asked Feb 01 '18 16:02

Alexandre SIRKO


2 Answers

It appears that the problem is caused by angularFire2. I had the same issue but was able to fix it by putting the following code in my main.ts file.

if ('serviceWorker' in navigator && environment.production) {
  navigator.serviceWorker.register('/ngsw-worker.js');
}
like image 91
user4426017 Avatar answered Sep 25 '22 22:09

user4426017


a also had to change my base url to index.html, otherwise the service worker can not work by design:

index.html:

<base href="index.html">
like image 28
Sebastian Viereck Avatar answered Sep 24 '22 22:09

Sebastian Viereck