简体   繁体   English

Angular Service Worker,缓存离线应用的 api 调用

[英]Angular Service Worker, caching api calls for offline app

I'm trying to make the service worker in angular work with API requests.我正在尝试使服务工作者通过 API 请求进行角度工作。 I'd like the app to work offline and I've the below config:我希望该应用离线工作,并且我有以下配置:

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

Here is what the xhr tab looks like when I'm offline:这是我离线时 xhr 选项卡的样子:

在此处输入图片说明

and this is the content of the user request:这是用户请求的内容:

在此处输入图片说明

As you can see the API calls for user don't resolve.如您所见,用户的 API 调用无法解析。

This is what the response of user looks like when online:这是user在线时的响应:

在此处输入图片说明

Try this:试试这个:

  • Go to your application tab -> Clear Storage -> Clear Site Data.转到您的应用程序选项卡 -> 清除存储 -> 清除站点数据。
  • Change your DataGroups array from this:从此更改您的 DataGroups 数组:

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

To this:对此:

"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"
      }
    }
  ]
  • save and build your app in PROD ,PROD保存和构建您的应用程序,
  • visit the page the first time.第一次访问该页面。
  • disable network禁用网络
  • refresh your page刷新页面

如果您使用“策略”:“性能”删除超时属性

To handle API data offline you have to use Offline Storage (that is store data in web browser after initial API call), recently I have used Local Forage in my application.要离线处理 API 数据,您必须使用离线存储(即在初始 API 调用后将数据存储在 Web 浏览器中),最近我在我的应用程序中使用了 Local Forage。 Find the linkS below:找到以下链接:

https://github.com/Alorel/ngforage#types-and-polyfills https://github.com/Alorel/ngforage#types-and-polyfills

https://developers.google.com/web/fundamentals/instant-and-offline/web-storage/offline-for-pwa https://developers.google.com/web/fundamentals/instant-and-offline/web-storage/offline-for-pwa

"dataGroups": [ { “数据组”:[{

        "name": "TestApi",
        "urls": [
            "https://api.chucknorris.io/jokes/random",
            "https://localhost:4377/api/home/getNames/"
        ],
        "cacheConfig": {
            "strategy": "freshness",
            "maxSize": 20,
            "maxAge": "1h",
            "timeout": "5s"
        }
    },
    {
        "name": "TestAPI",
        "urls": [
            "https://localhost:4377/api/test/getList",
            "https://localhost:4377/api/template/start/"
        ],
        "cacheConfig": {
            "strategy": "performance",
            "maxSize": 20,
            "maxAge": "2h",
            "timeout": "5s"
        }
    }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM