Search code examples
angulargoogle-chromeservice-workerangular-service-worker

Angular service worker not caching post requests


Does anyone know if the angular service worker is not caching post requests ? I integrated it in my app using @angular 5.2.5 and it caches some requests but some of them are posts and those ones he doesn't cache even if they match the urls patern which I specified. Here is my 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/**"
        ],
        "urls": [
          "**/fonts.googleapis.com/**/*",
          "**/www.gstatic.com/**/*"
        ]
      }
    }
  ],
  "dataGroups": [
    {
      "name": "api-performance",
      "urls": [
        "/api/attachments/*"
      ],
      "cacheConfig": {
        "maxSize": 200,
        "maxAge": "7d",
        "timeout": "10s",
        "strategy": "performance"
      }
    },
    {
      "name": "api-freshness",
      "urls": [
        "/api/*",
        "/api/**/*",
        "/api/**/**/*",
        "/api/**/**/**/*",
        "/api/views/*",
        "/api/app-structure",
        "/api/workflow/view",
        "/api/issues/*",
        "/.well-known",
        "/certs"
      ],
      "cacheConfig": {
        "maxSize": 200,
        "maxAge": "2d",
        "timeout": "10s",
        "strategy": "freshness"
      }
    }
  ]
}

This are added just for testing because /views is not cached and that one is a post request and I tried to add whildcards to match the pattern but no success on that "/api/*", "/api/**/*", "/api/**/**/*", "/api/**/**/**/*".

I have problems testing my website in production build with chrome because for our internal production like environment we are using selfsigned certificated does anyone know a workaround for this ? I tried this response but chrome still shows the errors and service worker is not running


Solution

  • Angular Service Worker does not cache post requests, and seems to me as a correct design principle. If you are trying to add a record while offline and want to sync with server data when coming online, you can try using IndexedDb or PouchDb(JavaScript database that syncs seamlessly).

    Here are some of the reference articles to get started with PouchDB