angular 6 service worker dataGroups not working

对着背影说爱祢 提交于 2021-01-28 08:11:06

问题


Hi I am trying to deploy a background Service worker that makes calls to api in some time interval in this case 3s, but it seams not to work, when I go to application I am getting no requests to server.

My data group in ngsw-config.json stands:

  "dataGroups": [
    {
      "name": "app-service-list",
      "urls": [ "http://localhost:5000/api/TcpCommunication/GetCabinsService" ],
      "cacheConfig": {
        "maxSize": 5,
        "maxAge": "3s",
        "strategy": "performance"
      }
    }
  ]

And when I do http-server -p 8090 web page works, but not sending any requests, I have in my app.module.ts line:

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

and in app.json:

in my angular json

"serviceWorker": true

I've added that service worker to existing angular application by ng add @angular/pwa@0.6.8

So what am I missing?


回答1:


When you are using the service workers, they acting in your case as a cache level. which means that it will cache or preform as you configure them to.
It won't do it by it self just from your configuration, you have to actually get the HTTP Calls from the api. You wanna take a look service-worker-communications from the official docs.

In short, you have to create a new service, which consume the SwUpdate service, and in your case, maybe register an interval which will get the data from the API, and check if your SW works.



来源:https://stackoverflow.com/questions/51613883/angular-6-service-worker-datagroups-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!