服务工作者可以缓存POST请求吗? [英] Can service workers cache POST requests?

查看:702
本文介绍了服务工作者可以缓存POST请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在fetch事件中缓存服务工作者的POST请求。



我使用 cache.put(event.request,response ),但返回的承诺被拒绝, TypeError:无效的请求方法POST。



<当我尝试使用相同的POST API时, caches.match(event.request)给了我undefined。



但是当我为GET方法做同样的事情时,它起作用了: caches.match(event.request)对于GET请求给了我一个回复。



服务工作者可以缓存POST请求吗?
如果他们不能,我们可以用什么方法让应用真正脱机?

解决方案

你可以'使用Cache API缓存POST请求。请参阅 https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#cache-put -method (第2.2点)。



规范库中有相关的讨论: https://github.com/slightlyoff/ServiceWorker/issues/693



一个有趣的解决方案是ServiceWorker Cookbook: https://serviceworke.rs/request-deferrer.html
基本上,解决方案序列化请求到IndexedDB。


I tried to cache a POST request in a service worker on fetch event.

I used cache.put(event.request, response), but the returned promise was rejected with TypeError: Invalid request method POST..

When I tried to hit the same POST API, caches.match(event.request) was giving me undefined.

But when I did the same for GET methods, it worked: caches.match(event.request) for a GET request was giving me a response.

Can service workers cache POST requests? In case they can't, what approach can we use to make apps truly offline?

解决方案

You can't cache POST requests using the Cache API. See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#cache-put-method (point 2.2).

There's a related discussion in the spec repository: https://github.com/slightlyoff/ServiceWorker/issues/693

An interesting solution is the one presented in the ServiceWorker Cookbook: https://serviceworke.rs/request-deferrer.html Basically, the solution serializes requests to IndexedDB.

这篇关于服务工作者可以缓存POST请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆