【问题标题】:service worker server image cache服务工作者服务器图像缓存
【发布时间】:2023-03-31 23:28:01
【问题描述】:

您好,我是 Service Worker 的新手,我为我的 Angular 项目添加了一个 Service Worker。

我通过了。 https://angular.io/guide/service-worker-introanguar 医生。

我的服务人员正在工作,它只缓存应用程序外壳。大多数图像是使用服务器请求请求并显示在我的应用程序中的。如何使用服务人员在我的应用程序中缓存这些图像。 *图像可以不时更改。

示例网址:http://localhost:1337/templates/viewWebImages?userId=5b20990f60dcc306c398b322&appId=5b851aff5f78be788b1050e5&1536906941235&images=secondNavi/3.png

【问题讨论】:

  • 你的图片网址格式是什么?
  • 编辑您的问题并提及您的图片网址格式。我只是在为您的案例编写解决方案。
  • localhost:1337 是单独的服务还是服务于 Angular 应用程序本身的服务?
  • 它是一个单独的服务器

标签: angular service-worker


【解决方案1】:

在ngsw-config.json 中,您必须指定图片的网址格式并为图片设置cacheConfig。

{
  "index": "/index.html",
  "assetGroups": [{
    "name": "app",
    "installMode": "prefetch",
    "resources": {
      "files": [
        "/favicon.ico",
        "/index.html"
      ]
    }
  }, {
    "name": "assets",
    "installMode": "lazy",
    "updateMode": "prefetch",
    "resources": {
      "files": [
        "/assets/**"
      ]
    }
  }],
  "dataGroups": [
    {
      "name": "images-api",
      "urls": [
        "http://localhost:1337/templates/viewWebImages**"
      ],
      "cacheConfig": {
        "strategy": "freshness",
        "timeout":"10s",
        "maxAge": "1d",
        "maxSize": 100
      }
    }
  ]
}

【讨论】:

  • 它不工作。它显示了网络中文件类型为“fetch”的缓存文件,并且在应用程序离线时丢失。基本上不缓存。 ;/
  • 对不起,图片url有错误,再看答案,检查图片url为“localhost:1337/templates/viewWebImages?**”
  • 还是没有运气;/
  • 您可能需要用于图像 api 的数据组,请再次检查。
  • 它的工作兄弟。我添加了一些预加载来从我的 ts 组件加载图像并附加到我的 DOM。链接stackoverflow.com/questions/10863658/… 及其工作:D
猜你喜欢
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
  • 2016-10-15
  • 2020-01-08
  • 1970-01-01
相关资源
最近更新 更多