Can Service Workers respond to synchronous XHR requests?

后端 未结 3 1828
[愿得一人]
[愿得一人] 2021-01-03 23:42

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

3条回答
  •  迷失自我
    2021-01-04 00:21

    In Theory

    Yes, service workers should be able to respond to synchronous XHR requests. This isn't explicitly stated in the specifications, but there's no exception that would cause synchronous XHR requests to be treated differently, and the W3C Web Platform Tests (WPT) suite has a test case to verify that it's supported: wpt/service-workers/service-worker/fetch-request-xhr-sync.https.html.

    In Practice

    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.

    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.

提交回复
热议问题