3

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

Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
Nicu
  • 3,476
  • 4
  • 23
  • 43

2 Answers2

5

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

Jeshwel
  • 141
  • 6
  • The Service Worker cache won't allow POST requests to be cached https://w3c.github.io/ServiceWorker/#cache-put so this is the only way forward. – JayChase Nov 22 '18 at 02:49
1

This is not an Angular issue, it's due to how service workers function. For now, service workers don't cache any post requests. Follow this discussion for more details https://github.com/w3c/ServiceWorker/issues/977

Gabriel Vasile
  • 2,110
  • 1
  • 14
  • 26