0

According to the official documents, I try to build a PWA work in the offline environment, I can do this with pure PWA without angular-cli.

But I followed official documents, It can't run offline. It's that document https://angular.io/guide/service-worker-getting-started#getting-started-with-service-workers

My angular-cli version: @angular/cli@8.3.17

My code https://github.com/unnhao/pwa-test

Narm
  • 10,677
  • 5
  • 41
  • 54
LiHao
  • 290
  • 3
  • 13

1 Answers1

0

Try manually registering the service worker. In your src/main.ts change the following:

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));

to.........

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

Sorry for the formatting, this is my first official answer. I had this same issue and tried a bunch of different things. This is the only solution that worked for me. I originally found the solution here: https://github.com/angular/angular-cli/issues/13351