0
votes

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/[email protected]

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

1
Show us your code - Tony Ngo
Does my answer solve your problem ? - Tony Ngo
Still not work. - LiHao
r u running your project on localhost? Let's ng build --prod. - Hoàngg

1 Answers

0
votes

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