当api位于不同域时如何通过Angular Service Worker缓存api响应

时间:2018-07-12 09:21:49

标签: angular asp.net-web-api2 angular-service-worker

我无法通过Angle Service Worker缓存我的主数据api 大多数示例显示dataGroup中的url在同一域中,因此仅“ / catalog”有效 但我的api托管在另一个域上

让其在http://x.com/api/catalog

可用

以下是ngsw-config.json

{
  "index": "/index.html",
  "assetGroups": [{
    "name": "app",
    "installMode": "prefetch",
    "resources": {
      "files": [
        "/favicon.ico",
        "/index.html"
      ],
      "versionedFiles": [
        "/*.bundle.css",
        "/*.bundle.js",
        "/*.chunk.js"
      ]
    }
  }, {
    "name": "assets",
    "installMode": "lazy",
    "updateMode": "prefetch",
    "resources": {
      "files": [
        "/assets/**"
      ],
      "urls": [
        "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
      ]
    }
  }],
  "dataGroups": [{
    "name": "xyz",
    "urls": ["http://x.com/api/catalog"],
    "cacheConfig": {
      "strategy": "freshness",
      "maxSize": 15,
      "maxAge": "1h",
      "timeout": "5s"
    }
  }]
}

1 个答案:

答案 0 :(得分:1)

dataGroups中的上述代码有效

唯一的事情是在加载应用程序后刷新或打开新选项卡供服务工作者缓存

在以下链接中给出了原因

https://blog.angular-university.io/service-workers/

服务工作者为什么不立即处于活动状态?部分