Service Service能否响应同步XHR请求? [英] Can Service Workers respond to synchronous XHR requests?

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

问题描述

我想使用Service Workers来增强现有的网站。特别是,我希望通过让Service Workers在实际资源不可用时使用占位符资源响应请求来添加更好的脱机支持。这种方法一直在运作,但我遇到了一个障碍。站点中有一些地方使用同步 XHR请求来加载某些资源,而我的服务工作者在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已经成功安装。

Here is a basic example, attempting to use a Service Worker to rewrite a few requests. Assume that the current Service Worker has already been successfully installed.

navigator.serviceWorker.register('/service-worker.js');

// Asynchronous request using traditional API
const axhr = new XMLHttpRequest();
axhr.open('GET', '/__so-example__', true);
axhr.onload = event => {
  console.log("A-XHR: " + axhr.responseText);
}
axhr.send();

// Synchronous request using traditional API
const sxhr = new XMLHttpRequest();
sxhr.open('GET', '/__so-example__', false);
sxhr.onload = event => {
  console.log("S-XHR: " + sxhr.responseText);
}
sxhr.send();

// Asynchronous request using new API
fetch('/__so-example__').then(response => response.text()).then(text => {
  console.log("FETCH: " + text);
});



服务工作者



Service Worker

self.addEventListener('fetch', event => {
  if (event.request.url.includes('__so-example__')) {
    event.respondWith(new Response('Dummy Value'));
  }
});



Chrome中的结果



Results in Chrome

A-XHR: Dummy Value
S-XHR: <not intercepted; request fails when offline>
FETCH: Dummy Value



Firefox中的结果



Results in Firefox

A-XHR: Dummy Value
S-XHR: Dummy Value
FETCH: Dummy Value

服务工作者应该可以响应同步XHR请求吗?我可以想象这实现起来很复杂,并且会理解它是否不受支持,但Firefox似乎已经做到了。这是一个Chrome错误,是规范中尚未解决的问题,还是Firefox只提供了比所需更多的功能?

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, but Firefox seems to have done it. Is this a Chrome bug, an unresolved issue with the specification, or is Firefox simply providing more functionality than required?

W3C服务工作者规范(工作草案) WHATWG获取规范(生活标准),但我还没有完成它们的解密。我非常感谢有关规范如何描述是否应该支持的明确解释,和/或有关指定或实现此行为的讨论的任何参考。

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 decyphering them. I would greatly appreciate a clear 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.

推荐答案

根据 XMLHttpRequest规范获取同步和异步请求的过程相同因此,从理论上讲,它应该被拦截,但同步XHR已被弃用所以我不会希望Chrome能解决此问题。

According to XMLHttpRequest spec, the fetch procedure for both synchronous and asynchronous requests is the same so, in theory, it should be intercepted but Synchronous XHR has been deprecated so I would not expect Chrome to fix this.

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

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