Angular 6 PWA与节点不起作用 [英] Angular 6 PWA with Node not working

查看:124
本文介绍了Angular 6 PWA与节点不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Angular 6应用程序,当与
http-server --port 8080 命令一起使用时,它可以工作并注册软件,如下所示:

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:

但是,当我从Node / Express应用程序提供文件时,它们应该被提供。服务人员不会注册,尝试从localhost以及Heroku运行该应用程序,但这是相同的。应用程序否则起作用。知道是什么原因造成的吗?

But when I serve the files from my Node / Express application that they are meant to be served. The service worker won't register, tried running the app from localhost and also from Heroku but it's the same. Application works otherwise. Any idea what can cause this?

推荐答案

我认为这个问题与 @ angular / cli 注册服务工作者时使用的路径有关,我发现注册服务 main.ts 中的工作人员更可靠:

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:

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

或者,看着最近的评论,您可以在 app.modules.ts

Or, looking at this recent comment you can manually modify the path in app.modules.ts

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

这篇关于Angular 6 PWA与节点不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆