【问题标题】:angular 6 service worker dataGroups not workingangular 6 service worker dataGroups 不工作
【发布时间】:2019-01-07 21:06:51
【问题描述】:

您好,我正在尝试部署一个后台 Service Worker,它在这种情况下 3 秒内以某个时间间隔调用 api,但它似乎无法正常工作,当我转到应用程序时,我没有收到对服务器的请求。

我在 ngsw-config.json 中的数据组代表:

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

当我做 http-server -p 8090 网页工作,但不发送任何请求时, 我的 app.module.ts 行中有:

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

在 app.json 中:

在我的角度 json 中

"serviceWorker": true

我已经通过 ng add @angular/pwa@0.6.8 将该服务工作者添加到现有的 Angular 应用程序中

那么我错过了什么?

【问题讨论】:

  • 不确定您要达到的目标,但 dataGroups 不会预取请求。
  • 我需要一些独立于 UI 的东西,每 3 分钟向 api 询问一些信息:
  • 你应该为此创建一个服务。
  • 只有你可以预取的assetGroups,它会在应用加载时缓存资源。数据组真的很懒惰。

标签: angular service-worker angular6


【解决方案1】:

当您使用服务工作者时,它们在您的情况下充当缓存级别。这意味着它会在您配置它们时缓存或预执行。
它不会仅从您的配置中自行完成,您必须实际从 api 获取 HTTP 调用。你想看看官方文档中的service-worker-communications

简而言之,您必须创建一个新服务,该服务使用 SwUpdate 服务,并且在您的情况下,可能注册一个从API,并检查您的 SW 是否正常工作。

【讨论】:

    猜你喜欢
    • 2020-12-06
    • 2020-11-28
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多