Library Affected:
workbox-build
Browser & Platform:
all browsers
Issue or Feature Request Description:
I'm having this error:
Uncaught (in promise) TypeError: Request method 'POST' is unsupported
when using this code
workbox.routing.registerRoute(
new RegExp('^https://bam.nr-data.net/*'),
workbox.strategies.networkFirst({
plugins: [
new workbox.expiration.Plugin({
maxEntries: 20,
maxAgeSeconds: 24 * 60 * 60
}),
new workbox.backgroundSync.Plugin('nrPost', {
maxRetentionTime: 24 * 60
})
]
}),
'POST'
);
I saw in some other related issues that we can't use it with POST methods but in your docs, the first example it shows one with POST:
const bgSyncPlugin = new workbox.backgroundSync.Plugin('myQueueName', {
maxRetentionTime: 24 * 60 // Retry for max of 24 Hours
});
workbox.routing.registerRoute(
/\/api\/.*\/*.json/,
workbox.strategies.networkOnly({
plugins: [bgSyncPlugin]
}),
'POST'
);
So, can workbox cache POST request?
The Cache Storage API doesn't support using Request objects with a method of POST as keys in a cache. So Workbox doesn't support that, either.
The examples in the docs show using the workbox.strategies.networkOnly (which does not interact with the cache) strategy as the handler for that route, along with the workbox.backgroundSync.Plugin to retry the request if it fails. That's the model to follow.
Your workbox.strategies.networkFirst strategy will end up trying to add the POST request to the cache if there's a response from the network, as explained in this diagram: https://developers.google.com/web/tools/workbox/modules/workbox-strategies#network_first_network_falling_back_to_cache
So what is the work around to this?
I have the same code as above and getting same error. How do i resolve this ?
Most helpful comment
The Cache Storage API doesn't support using
Requestobjects with a method ofPOSTas keys in a cache. So Workbox doesn't support that, either.The examples in the docs show using the
workbox.strategies.networkOnly(which does not interact with the cache) strategy as the handler for that route, along with theworkbox.backgroundSync.Pluginto retry the request if it fails. That's the model to follow.Your
workbox.strategies.networkFirststrategy will end up trying to add thePOSTrequest to the cache if there's a response from the network, as explained in this diagram: https://developers.google.com/web/tools/workbox/modules/workbox-strategies#network_first_network_falling_back_to_cache