0

hi I'm new to service workers and I have added a service worker for my angular project.

I went through. https://angular.io/guide/service-worker-intro anguar doc.

my service worker is working and it only cache the app shell. the most of images are requested using and a server request and showing in my application. how can I cache these images in my application using service workers. *the images can be changed time to time.

example URL: http://localhost:1337/templates/viewWebImages?userId=5b20990f60dcc306c398b322&appId=5b851aff5f78be788b1050e5&1536906941235&images=secondNavi/3.png

1 Answers1

1

In ngsw-config.json you must specify the url pattern of your images and set cacheConfig for the images.

{
  "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
      }
    }
  ]
}
Fartab
  • 4,725
  • 2
  • 26
  • 39