编程语言
首页 > 编程语言> > javascript-Angular Service Worker,为离线应用程序缓存api调用

javascript-Angular Service Worker,为离线应用程序缓存api调用

作者:互联网

我正在尝试使服务工作者能够与API请求一起工作.
我希望该应用离线运行,并具有以下配置:

    {
        "name": "api",
        "urls": ["https://x.com/**"],
        "cacheConfig": {
            "strategy": "performance",
            "maxSize": 20,
            "maxAge": "365d",
            "timeout": "5s"
        }
    }

这是我离线时xhr标签的外观:

enter image description here

这是用户请求的内容:

enter image description here

如您所见,用户的API调用无法解析.

这是用户在线时的响应:

enter image description here

解决方法:

尝试这个:

>转到您的应用程序标签->清除存储->清除站点数据.
>从此更改您的DataGroups数组:

{
    "name": "api",
    "urls": ["https://x.com/**"],
    "cacheConfig": {
        "strategy": "performance",
        "maxSize": 20,
        "maxAge": "365d",
        "timeout": "5s"
    }
}

对此:

"dataGroups": [
    {
      "name": "api-performance",
      "urls": [
        "/user" //<=========== I know you want all API calls to be cached, but try this first and see
      ],
      "cacheConfig": {
        "strategy": "performance",
        "maxSize": 100,
        "maxAge": "3d"
      }
    }
  ]

>在PROD中保存并构建您的应用,
>第一次访问该页面.
>禁用网络
>刷新页面

标签:angular,service-worker,angular5,angular-service-worker,javascript
来源: https://codeday.me/bug/20191110/2013451.html