Reputation: 121
is it possible to cache the response of POST request using workbox? my application uses POST request instead of GET to fetch list data , since user credentials are sent along with the request.
Upvotes: 6
Views: 6425
Reputation: 453
You can cache POST request with the following syntax:
workbox.routing.registerRoute(
<url>,
<workbox strategy>,
'POST'
);
Official Documentation: https://developer.chrome.com/docs/workbox/modules/workbox-routing/#defining-a-route-for-non-get-requests
Upvotes: 7
Reputation: 669
Let me clarify S. Esteves' answer.
Browser cache storage doesn't has an ability to persist POST requests as a keys (see spec here).
If innerRequest’s url's scheme is not one of "http" and "https", or innerRequest’s method is not
GET
, return a promise rejected with a TypeError.
Workbox lets you intercept POST requests (as service worker does on fetch event with GET requests) by passing 'POST' as third parameter to registerRoute function, then you can put some custom logic into handler (but even manually you still unable to save response into cache storage, as it is not supported).
So direct use of Workbox strategies which do cache responses will end up with error (see example here).
So options here are:
Quote from here:
HTTP caching is applicable only to idempotent requests, which makes a lot of sense; only idempotent and nullipotent requests yield the same result when run multiple times. In the HTTP world, this fact means that GET requests can be cached but POST requests cannot
However, there can be cases where an idempotent request cannot be sent using GET simply because that request exceeds the limits imposed by popular Internet software.
workbox.routing.registerRoute(
/api/, // Change to match your endpoint URL.
async ({event, url}) => {
// See https://developers.google.com/web/tools/workbox/guides/route-requests#handling_a_route_with_a_custom_callback
// Do something here. What it is up to you.
// Eventually, return a Response object, or else your request will fail.
return response;
},
'POST' // Opt-in to matching POST requests. Only GET is matched by default.
);
Upvotes: 15