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.)
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.
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
.
Service workers can only respond to synchronous XHR requests coming from the main thread in Firefox and pre-Chromium versions of Edge. It doesn't work in Chrome, Safari, or current versions of Edge and other Chromium browsers. Chromium was once intending to support it, but five years later, it still hasn't happened.
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://wpt.live/service-workers/service-worker/fetch-request-xhr-sync.https.html
All browsers do seem to support service workers for synchronous XHR requests sent from secondary threads such as Web Workers, but there isn't a specific WPT test to validate this.