gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:19:12 24 4
gpt4 key购买 nike

我正在尝试让服务 worker 以 Angular 方式处理 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 调用未解析。

这是 user 在线时的响应:

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 中保存并构建您的应用,
  • 第一次访问该页面。
  • 禁用网络
  • 刷新页面

关于javascript - Angular Service Worker,缓存离线应用程序的api调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48419769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com