Skip to content

Add fetch() support to LongPoll when XMLHTTPRequest is not available #6181

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Thomas101
Copy link

Hi, we noticed that when opening a socket in a ServiceWorker and using LongPoll, the socket fails because neither XMLHTTPRequest or XDomainRequest are available in the ServiceWorker scope.

I realise this is a bit of an edge case, as if you have ServiceWorker support, you probably already have native WebSocket support. In our instance, we sometimes fall back to LongPoll when network conditions block connections.

To work around this, I've added support for using fetch, which is in the global scope of a ServiceWorker. To ensure we don't break any existing setups, LongPoll requests still prefer XDomainRequest and XMLHTTPRequest in the same ordering as before; this only acts as a fallback when neither are available.

@SteffenDE
Copy link
Contributor

Very nice! I'd love to see an extra test here that mocks XMLHTTPRequest to undefined or whatever and then expects the respecting fetch calls.

@Thomas101
Copy link
Author

Sure! I've added those in 👍

@SteffenDE SteffenDE requested a review from chrismccord April 11, 2025 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants