問題描述
我想使用 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 請求嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!