我尝试在 fetch 事件的服务工作人员中缓存 POST 请求。
I used cache.put(event.request, response)
,但返回的承诺被拒绝TypeError: Invalid request method POST.
.
当我尝试使用相同的 POST API 时,caches.match(event.request)
给了我未定义的信息。
但是当我对 GET 方法执行相同操作时,它起作用了:caches.match(event.request)
GET 请求给了我一个响应。
Service Worker 可以缓存 POST 请求吗?
如果不能,我们可以用什么方法让应用程序真正离线?
您无法使用 Cache API 缓存 POST 请求。看https://w3c.github.io/ServiceWorker/#cache-put https://w3c.github.io/ServiceWorker/#cache-put(第 4 点)。
规范存储库中有相关讨论:https://github.com/slightlyoff/ServiceWorker/issues/693 https://github.com/slightlyoff/ServiceWorker/issues/693
ServiceWorker Cookbook 中提出了一个有趣的解决方案:https://serviceworke.rs/request-deferrer.html https://serviceworke.rs/request-deferrer.html基本上,该解决方案将请求序列化到 IndexedDB。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)