简体   繁体   中英

Angular Progressive Web App(PWA) offline not work

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM