Skip to content
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

.. Uncaught (in promise) Error: Browser-only version of superagent could not find XHR #1731

Closed
HorrorBest opened this issue Jun 21, 2022 · 11 comments

Comments

@HorrorBest
Copy link

I got this error in console while I Was working on something... "Uncaught (in promise) Error: Browser-only version of superagent could not find XHR"
I was using SuperAgent without any other things (bundles/js script) that depend on it... and This is my code if needed
NOTE: I need it to be used in browser (not use Node JS) also I used the Polyfill that I got from the wiki and I got the same error

HTML:
<script src="https://cdn.jsdelivr.net/npm/js-polyfills@0.1.43/polyfill.js"></script>
<script src="https://cdn.jsdelivr.net/npm/superagent@7.1.6/dist/superagent.js"></script>

JS FILE:
const server = async () => {
let serverlist = await
superagent.post('request adresse')
.send({email: 'email@email.com', password: 'password1234'})
.set('content-type', 'application/x-www-form-urlencoded; charset=UTF-8');
console.log(serverlist.text);
}

server();

NOTE: Email and Password are fake here In the real code I got a real email and password.

@schnuet
Copy link

schnuet commented Jun 23, 2022

As a workaround, reverting to 7.1.5 will do the job:

Replace the line <script src="https://cdn.jsdelivr.net/npm/superagent@7.1.6/dist/superagent.js"></script>
with <script src="https://cdn.jsdelivr.net/npm/superagent@7.1.5/dist/superagent.js"></script>


There seems to be a bug that came in with the newest version (7.1.6) in the method request.getXHR in client.js; a condition was deleted (in commit 59518b0) that is seemingly causing the error now:

line in 7.1.5: if (root.XMLHttpRequest && (!root.location || root.location.protocol !== 'file:' || !root.ActiveXObject)) {
line in 7.1.6: if (root.XMLHttpRequest && (!root.location || root.location.protocol !== 'file:') {

I got the error while trying to use superagent in my electron app. The main file is loaded via file protocol, up until 7.1.5 the condition would evaluate to true and I could use the standard XMLHttpRequest. Now, I am getting the same error as HorrorBest.

Is there a reason why documents loaded with the file: protocol are being blocked? Or was that an unintended consequence?

@titanism
Copy link
Collaborator

It is most likely due to !root.ActiveXObject being removed in v7.1.6 vs. v7.1.5. In Electron, it doesn't seem like root is properly being set, therefore it's hitting the other part of that conditional, which !root.ActiveXObject is true.

  if (
    root.XMLHttpRequest &&
    (!root.location ||
      root.location.protocol !== 'file:' ||
      !root.ActiveXObject)
      root.location.protocol !== 'file:')
  ) {
    return new XMLHttpRequest();

This is the code in question.

In v7.1.6 the line !root.ActiveXObject is removed.

The value of root is determined here:

let root;
if (typeof window !== 'undefined') {
  // Browser window
  root = window;
} else if (typeof self === 'undefined') {
  // Other environments
  console.warn(
    'Using browser-only version of superagent in non-browser environment'
  );
  root = this;
} else {
  // Web Worker
  root = self;
}

@titanism
Copy link
Collaborator

Is it possible you can evaluate that code block in Electron and try to debug what's wrong here?

@titanism
Copy link
Collaborator

This test seems to work OK without throwing an error: https://jsfiddle.net/6edrz78y/

@titanism
Copy link
Collaborator

We've tested locally the issue with Electron and confirmed it. Thanks for reporting this.

We are going to deprecate v7.1.6 and release v7.1.7 shortly.

@titanism
Copy link
Collaborator

v7.1.6 has been deprecated as it should have been a major semver release anyways. We are releasing v8.0.0 shortly.

@titanism
Copy link
Collaborator

@titanism
Copy link
Collaborator

Just tested v8.0.0 locally with Electron, works fine!

@schnuet
Copy link

schnuet commented Jun 24, 2022

Updated to 8.0.0, works flawlessly in my environment, too. Thank you very much for your work. :)

@pinggi
Copy link

pinggi commented Oct 4, 2022

We've tested locally the issue with Electron and confirmed it. Thanks for reporting this.

We are going to deprecate v7.1.6 and release v7.1.7 shortly.

Are you going to release the v7.1.7?

The package.json file with:
"superagent": "^7",

gives the error:

warning superagent@7.1.6: Please downgrade to v7.1.5 if you need IE/ActiveXObject support OR upgrade to v8.0.0 as we no longer support IE and published an incorrect patch version (see https://github.com/visionmedia/superagent/issues/1731)

I still need IE support.

@darioackermann
Copy link

darioackermann commented Oct 22, 2022

Doesn't look like 7.1.7 is released. If you require IE support, you'll have to lock the dependency to 7.1.5.
This is okay to do since ^7 will point to the newest release of 7 that is not major, and as 7.1.6 is the last one (and broken for your use-case), locking 7.1.5 is equivalent of what you intend to do

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

No branches or pull requests

5 participants