Service Worker 可以缓存 POST 请求吗? [英] Can service workers cache POST requests?

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

问题描述

我尝试在 fetch 事件中在 service worker 中缓存 POST 请求.

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

我使用了cache.put(event.request, response),但是返回的promise被TypeError: Invalid request method POST.拒绝了.

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

当我尝试使用相同的 POST API 时,caches.match(event.request) 给我未定义.

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

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

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

Service Worker 可以缓存 POST 请求吗?如果他们不能,我们可以使用什么方法使应用真正离线?

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

推荐答案

您无法使用缓存 API 缓存 POST 请求.请参阅 https://w3c.github.io/ServiceWorker/#cache-put(第 4 点).

You can't cache POST requests using the Cache API. See https://w3c.github.io/ServiceWorker/#cache-put (point 4).

spec 仓库中有相关讨论:https://github.com/slightlyoff/ServiceWorker/issues/693

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

ServiceWorker Cookbook 中提供了一个有趣的解决方案:https://serviceworke.rs/request-deferer.html基本上,该解决方案将请求序列化到 IndexedDB.

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

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

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