Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Service Worker with POST request

I use Angular 5.2.7 with Angular Service Worker 5.2.7.

I want to cache webservices calls. So, I've got with this config in my ngsw-config.json, for dataGroups :

"dataGroups": [{
  "name": "tasks-users-api",
  "urls": ["/api", "/rest"],
  "cacheConfig": {
    "strategy": "freshness",
    "maxSize": 20000,
    "maxAge": "1h",
    "timeout": "5s"
  }
}]

I try to cache this two URLs : https://randomuser.me/api/?results=20 ---> It's OK https://test.moodlecloud.com/webservice/rest/server.php ---> It's KO

The first one is cached as expected. It's a simple GET service. The second one isn't. It's a POST service with a form data sent as payload.

Any idea of what is wrong with that ?

like image 621
fabienbranchel Avatar asked Mar 04 '18 22:03

fabienbranchel


People also ask

How do you by pass the service worker for a particular request Angular?

To bypass the service worker, set ngsw-bypass as a request header, or as a query parameter. The value of the header or query parameter is ignored and can be empty or omitted.

What is Serviceworker in Angular?

At its simplest, a service worker is a script that runs in the web browser and manages caching for an application. Service workers function as a network proxy. They intercept all outgoing HTTP requests made by the application and can choose how to respond to them.

What is SwUpdate in Angular?

Subscribe to update notifications from the Service Worker, trigger update checks, and forcibly activate updates.

What is NGSW-config?

You must be wondering what's ngsw-config. json? It's a configuration file that is used by Angular's service worker to configure the settings.


1 Answers

Service Worker API does not allow POST/PUT requests.

In this blog article it is showed an example on how you can use Indexed Db (as suggested by Roman Gherta) to store POST/PUT requests.

This solution is also suggested on Mozilla resource.

like image 158
Francesco Avatar answered Oct 13 '22 02:10

Francesco