tomastrajan / angular-ngrx-material-starter

Angular, NgRx, Angular CLI & Angular Material Starter Project
https://tomastrajan.github.io/angular-ngrx-material-starter
MIT License
2.83k stars 917 forks source link

Trouble getting service worker to appear #458

Closed toddtsic closed 5 years ago

toddtsic commented 5 years ago

Trying to add push notification capability to template (?future feature)

More likely this is my inexperience with service workers, but I cannot get a service worker to appear (chrome inspect | application).

Here are the steps I've taken

I first updated all the packages (ncu -u, npm update, followed by npm install)

then: ng add @angular/pwa --project angular-ngrx-material-starter ng build --prod http-server -p 8080 -c-1 dist

gives 404 error when running under http-server -p 8080 -c-1 dist to the /assets/i18n/en.json file, corrected by environment.prod.ts edit:

  // i18nPrefix: '/angular-ngrx-material-starter',
  i18nPrefix: '',

then

http-server -p 8080 -c-1 dist

when inspecting in chrome, I see no service worker despite the dist folder containint ngsw-worker.js and ngsw.json

Any push in the right direction greatly appreciated.

toddtsic commented 5 years ago

Update: I have tried something mentioned by Rafael Crescenzi Argentan on https://github.com/angular/angular-cli/issues/8779

He suggests registering the service worker in main.js:

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

And it does the trick. Not sure why, nor if this is a stable solution...