我试图在获取事件时在服务工作者中缓存 POST 请求。
我使用了 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 请求吗?如果他们做不到,我们可以使用什么方法让应用程序真正离线?
原文由 Aniket 发布,翻译遵循 CC BY-SA 4.0 许可协议
您不能使用 Cache API 缓存 POST 请求。请参阅 https://w3c.github.io/ServiceWorker/#cache-put (第 4 点)。
规范存储库中有相关讨论: https ://github.com/slightlyoff/ServiceWorker/issues/693
ServiceWorker Cookbook 中提供了一个有趣的解决方案: https ://serviceworke.rs/request-deferrer.html 基本上,该解决方案将请求序列化到 IndexedDB。