[Angular] Performance Caching Policy - Cache First, Network Last

If you want to cache API response by using angular service-worker, you can do it in:

src/ngsw-config.json:

{
  "index": "/index.html",
  "assetGroups": [{
    "name": "app",
    "installMode": "prefetch",
    "resources": {
      "files": [
        "/favicon.ico",
        "/index.html"
      ],
      "versionedFiles": [
        "/*.bundle.css",
        "/*.bundle.js",
        "/*.chunk.js"
      ]
    }
  }, {
    "name": "assets",
    "installMode": "lazy",
    "updateMode": "prefetch",
    "resources": {
      "files": [
        "/assets/**"
      ]
    }
  }],
  // cache for API data
  "dataGroups": [
    {
      "name": "lessons-api",
      "urls": [
        "/api/lessons"
      ],
      "cacheConfig": {
        "strategy": "performance", // cache first, network last
        "maxAge": "1d",
        "maxSize": 100
      }
    }
  ]
}

 

posted @ 2018-04-19 01:51  Zhentiw  阅读(255)  评论(0编辑  收藏  举报