Vugar Abdullayev
Vugar Abdullayev

Reputation: 2105

Angular service worker not caching apis in data groups array

Description

Angular service worker not caching apis in data groups array. My ngsw-config.json:

{
  "$schema": "./node_modules/@angular/service-worker/config/schema.json",
  "index": "/index.html",
  "assetGroups: [**],
  "dataGroups": [
    {
        "name": "all-apis",
        "urls": [
            "https://testapi.plusminus.az/mybank/**"
        ],
        "cacheConfig": {
            "maxSize": 100000000,
            "maxAge": "3d",
            "timeout": "1m"
        }
    }
]
}

When in offline mode the apis are not cached. Assets are working normally.

Reproduction

Please check website: https://test.plusminus.az/home

Upvotes: 1

Views: 1295

Answers (2)

sayinmehmet47
sayinmehmet47

Reputation: 561

I encountered the same issue where the Angular service worker was not caching API calls specified in the dataGroups array of my ngsw-config.json. After some research, I found that serving the app with caching disabled on the server side resolved the problem, and my app began caching the API responses.

To serve the directory containing your web files with http-server, run the following command:

http-server -p 8080 -c-1 dist/angular-pwa

The -c-1 option will disable server caching, and a server will run on port 8080, serving the production version of the application. if port 8080is busy then you can change to any port number.

Upvotes: 0

Pankaj Rupapara
Pankaj Rupapara

Reputation: 780

please remove timeout then you need to mention strategy under cacheConfig section

please read this angular document for more details. https://angular.io/guide/service-worker-config#datagroups

Upvotes: -1

Related Questions