Service Worker 可以响应同步的 XHR 请求吗? [英] Can Service Workers respond to synchronous XHR requests?

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

问题描述

我想使用 Service Worker 来增强现有网站.特别是,我想通过让 Service Worker 在实际资源不可用时使用占位符资源响应请求来添加更好的离线支持.这种方法一直有效,但我遇到了障碍.站点中有几个地方使用 同步 XHR 请求来加载某些资源,而我的 Service Worker 在 Chrome 中没有接收到它们的事件.(请不要建议消除同步 XHR 请求.这是可取的,但超出了范围.)

I would like to use Service Workers to enhance an existing web site. In particular, I would like to add better offline support by having Service Workers respond to requests with placeholder resources when the actual resources are not available. This approach had been working, but I've encountered an obstacle. There are a few places in the site where synchronous XHR requests are used to load certain resources, and my Service Worker doesn't receive events for them in Chrome. (Please don't suggest eliminating synchronous XHR requests. That's desired, but out-of-scope.)

Service Worker 是否可以响应同步 XHR 请求?我可以想象这实现起来很复杂,如果不支持它会理解的.W3C 服务工作者规范(工作草案)WHATWG Fetch Specification (Living Standard),但我还没有完成破译.我希望能解释一下规范如何描述是否应支持这一点,和/或对有关指定或实施此行为的讨论的任何引用.

Is it supposed to be possible for a Service Worker to respond to synchronous XHR requests? I could imagine this being complicated to implement, and would understand if it wasn't supported. A "correct" answer should exist between the W3C Service Workers Specification (Working Draft) and the WHATWG Fetch Specification (Living Standard), but I haven't finished deciphering them. I would appreciate an explanation of how the specifications describe whether or not this should be supported, and/or any references to discussions about specifying or implementing this behaviour.

推荐答案

理论上

是的,Service Worker 应该能够响应同步 XHR 请求.这在规范中没有明确说明,但是没有例外会导致同步 XHR 请求被区别对待,并且 W3C Web 平台测试 (WPT) 套件有一个测试用例来验证它是否受支持:wpt/service-workers/service-worker/fetch-request-xhr-sync.https.html.

从 2019 年 1 月起,Service Worker 可以在 Firefox 和 Edge 中响应同步 XHR 请求,但不能在 Chrome 或 Safari 中响应.Chrome 计划很快添加支持,但我们不知道 Safari 是否会这样做.

As of January 2019, service workers can respond to synchronous XHR requests in Firefox and Edge, but not in Chrome or Safari. Chrome is planning to add support soon, but we don't know if Safari ever will.

提供最新的浏览器支持矩阵 在 WPT.fyi.您可以在自己的浏览器中运行 WPT 测试用例 https://w3c-test.org/service-workers/service-worker/fetch-request-xhr-sync.https.html.

An up-to-date browser support matrix is available at WPT.fyi. You can run the WPT test case in your own browser at https://w3c-test.org/service-workers/service-worker/fetch-request-xhr-sync.https.html.

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

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