問(wèn)題描述
我想使用 Service Worker 來(lái)增強(qiáng)現(xiàn)有網(wǎng)站.特別是,我想通過(guò)讓 Service Worker 在實(shí)際資源不可用時(shí)使用占位符資源響應(yīng)請(qǐng)求來(lái)添加更好的離線支持.這種方法一直有效,但我遇到了障礙.站點(diǎn)中有幾個(gè)地方使用 同步 XHR 請(qǐng)求來(lái)加載某些資源,而我的 Service Worker 在 Chrome 中沒(méi)有接收到它們的事件.(請(qǐng)不要建議消除同步 XHR 請(qǐng)求.這是可取的,但超出了范圍.)
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 是否可以響應(yīng)同步 XHR 請(qǐng)求?我可以想象這實(shí)現(xiàn)起來(lái)很復(fù)雜,如果不支持它會(huì)理解的.W3C 服務(wù)工作者規(guī)范(工作草案) 和WHATWG Fetch Specification (Living Standard),但我還沒(méi)有完成破譯.我希望能解釋一下規(guī)范如何描述是否應(yīng)支持這一點(diǎn),和/或?qū)τ嘘P(guān)指定或?qū)嵤┐诵袨榈挠懻摰娜魏我?
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 應(yīng)該能夠響應(yīng)同步 XHR 請(qǐng)求.這在規(guī)范中沒(méi)有明確說(shuō)明,但是沒(méi)有例外會(huì)導(dǎo)致同步 XHR 請(qǐng)求被區(qū)別對(duì)待,并且 W3C Web 平臺(tái)測(cè)試 (WPT) 套件有一個(gè)測(cè)試用例來(lái)驗(yàn)證它是否受支持:wpt/service-workers/service-worker/fetch-request-xhr-sync.https.html
.
從 2019 年 1 月起,Service Worker 可以在 Firefox 和 Edge 中響應(yīng)同步 XHR 請(qǐng)求,但不能在 Chrome 或 Safari 中響應(yīng).Chrome 計(jì)劃很快添加支持,但我們不知道 Safari 是否會(huì)這樣做.
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.您可以在自己的瀏覽器中運(yùn)行 WPT 測(cè)試用例 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.
這篇關(guān)于Service Worker 可以響應(yīng)同步的 XHR 請(qǐng)求嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!