简体   繁体   English

Angular 6 PWA 节点不工作

[英]Angular 6 PWA with Node not working

I have a Angular 6 app that works and registers the SW when served with http-server --port 8080 command as you can see here:我有一个 Angular 6 应用程序,它可以在使用http-server --port 8080命令时运行并注册 SW,您可以在此处看到: 在此处输入图像描述

在此处输入图像描述

But when I serve the files from my Node / Express application that they are meant to be served.但是,当我从我的 Node / Express 应用程序提供文件时,它们应该被提供。 The service worker won't register, tried running the app from localhost and also from Heroku but it's the same. service worker 不会注册,尝试从 localhost 和 Heroku 运行应用程序,但都是一样的。 Application works otherwise.应用程序以其他方式工作。 Any idea what can cause this?知道是什么原因造成的吗?

在此处输入图像描述

I think this issue is to do with the path that @angular/cli uses when registering the service worker, I have found registering the service worker in main.ts to be more reliable: 我认为此问题与@angular/cli注册服务工作者时使用的路径有关,我发现在main.ts注册服务工作者更可靠:

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

Or, looking at this recent comment you can manually modify the path in app.modules.ts 或者,查看最近的评论,您可以在app.modules.ts手动修改路径

-ServiceWorkerModule.register('./ngsw-worker.js', { enabled: environment.production })
+ServiceWorkerModule.register('.ngsw-worker.js', { enabled: environment.production })

I also had a same issue that worked locally but not herokou, and as a result of various searches on the.net, I came to this page.我也有一个同样的问题,在本地有效但在 herokou 无效,并且由于在 .net 上进行了各种搜索,我来到了这个页面。

In my case, I finally solved it by adding --prod to the ng build.就我而言,我最终通过将--prod添加到 ng build 来解决它。 With --prod , the settings in environment.prod.ts will be reflected, the "serivice worker" in production in angular.json will be true, also "enabled" -> true in ServiceWorkerModule.register in app.module.ts , and service worker is now working.使用--prod ,将反映 environment.prod.ts 中的设置,angular.json 中生产中的“服务工作者”将为真,在app.module.ts中的ServiceWorkerModule.register中也为"enabled" -> true ,以及服务工人正在工作。 FYI.供参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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