如何使用Angular在本地运行Service Worker [英] How to run service worker locally with Angular
问题描述
我正在遵循angular的最佳做法来制作PWA。
进行生产构建( ng build --prod --aot
)之后,我还在 localhost上从dist运行服务工作者
:
http-server -p 8080 -c-1 dist
当我尝试与我的工人同步时Angular,使用:
I am following angular's best practice in order to make PWA.
After making production build (ng build --prod --aot
), I am also running the service worker from dist, on localhost
:
http-server -p 8080 -c-1 dist
When I am trying to sync the worker with my Angular, using:
navigator.serviceWorker.ready.then(function(swRegistration) {
console.log('swReady');
});
什么都没发生,而且似乎软件没有与Angular通信。
使用远程服务器(上传dist)确实可以。看来问题出在 ng serve
上。
我在做什么错?
Nothing happens, and seems that SW is not communicating with Angular.
Working with a remote server (uploading dist) does work. So seems that the problem is dist not working with ng serve
.
What am I doing wrong?
推荐答案
您不能通过 ng serve $ c $与服务工作者一起为Angular项目提供服务c>,因为Service Worker的文档规定需要 https 。在服务器上不使用https /的情况下运行它的唯一方法是使用
ng build
并运行本地http服务器来测试您的项目。
You can't serve your Angular project with service worker via ng serve
, as the documentation for Service Workers state that it requires https. The only way to run it without https/on a server, is to use ng build
and run the http-server locally to test your project.
由于ng serve无法与服务工作者一起使用,因此必须使用单独的HTTP服务器在本地测试您的项目。
Because ng serve does not work with service workers, you must use a separate HTTP server to test your project locally.
这篇关于如何使用Angular在本地运行Service Worker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!