-
-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add SendBeaconInterceptor #284
base: main
Are you sure you want to change the base?
Conversation
Hey, @hpohlmeyer! Thank you for working on this! Please, allow me some time to process the changes. Generally speaking, the Interceptors library is designed to intercept requests from various request clients (
Not entirely. XHR interceptor is here because there are DOM-like environments in Node.js, like JSDOM, which ship with XHR but are not browser. That's why we have a higher-level XHR interception implemented. I think there's also React Native that supports native XHR while not having global fetch but I may be mistaken. Since React Native is not Node.js, we cannot rely on The Fetch interceptor is a unique snowflake because it was designed for a single purpose: enable in-browser interception for scenarios where Service Worker cannot be registered in the browser (serving local |
Hey @kettanaito, sorry for the delayed response. I was on vacation last week.
Of course! Sorry for coming in with this rouge-PR instead of clarifying if you are open to see this implemented in the library. I wanted to see if it would be possible to intercept
Essentially it is an API to send http requests, that outlive the page. It is similar to a Thank you for explaining the motiviation behind the other interceptors, I was sure I did not get the full picture there. Your explanations helped a lot.
I think the The main motivation for this PR is, that I am working on a project where I am in need for low-level request interception. The library is well written and maintained and possibly the best low level interception library out there. But without the possibility to intercept I totally understand that this library is mainly a building block for MSW though, where the new Interceptor might not be as important. Let me know when you made a decision or need any information. |
Description
This PR adds a
SendBeaconInterceptor
for interceptingnavigator.sendBeacon()
calls.Important Details
Response data does not matter
sendBeacon
is a fire-and-forget call. It is a synchronous function that queues a network request, but does not wait for the response. It also does not provide any way to access the response. But mocking the response withrequest.respondWith()
still makes sense, if you do not want the request to be passed on to the originalsendBeacon
.No
response
eventsAs described above, there is no way to access the response of
sendBeacon
, which means we have no way to triggerresponse
events.sendBeacon
always returnstrue
when an interceptor is appliedThe original
sendBeacon
returnstrue
in case it sucessfully queued the network request andfalse
in case it did not. The criteria for queuing a request vary by the user agent and are not accessible in JavaScript. That means we can not know if queuing is possible before doing it. Therefore we can not return a sound value from oursendBeacon
mock.That problem does also affect routes, that do not mock the response, because we need to check if a mocked response has been defined. This is done asynchronously, but since
sendBeacon
needs to run synchronously, we can not wait until we know if the response has to be mocked.Usage in MSW
I am using
@msw/interceptors
for different purposes, but as stated in the readme, the main aim for this package is to provide interceptors for MSW. So, here are some words on that topic.Since
sendBeacon
is not implemented in Node, theSendBeaconInterceptor
is only relevant in Browsers. As far as I have seen, MSW handles the request interception in the service worker by listening tofetch
events. The fetch event is also able to interceptsendBeacon
calls, so MSW already supports beacon interception without the need forSendBeaconInterceptor
. But the same is true forFetchInterceptor
andXMLHttpRequestInterceptor
and they are still part of this package. I think theSendBeaconInterceptor
would make this package more feature-complete.I may have misunderstood the role of
@msw/interceptors
in MSW, if so, please let me know. 🙏