BetterTyped/hyper-fetch

Nuxt 3 - ReferenceError: XMLHttpRequest is not defined

johngerome opened this issue ยท 4 comments

Describe the bug
In Nuxt 3, running the request on the server side returns ReferenceError: XMLHttpRequest is not defined.
Is it possible to run a request on the server side ?
Any help would be much appreciated. Thank you.

To Reproduce
Steps to reproduce the behavior:

  1. Go to hyperfetch-nuxt-demo
  2. Check the logs on the Terminal

Expected behavior
No error should display

Screenshots
N/A

Desktop (please complete the following information):
N/A

Smartphone (please complete the following information):
N/A

Additional context
"nuxt": "3.2.0"
"@hyper-fetch/core": "^3.3.1"

prc5 commented

@johngerome Thanks for creating this ticket.

Is it possible to run a request on the server side ?

Yes, of course, this needs to work with the ssr!

I will check what's going on.

I will check what's going on.

Thank you @prc5. I greatly appreciate it

prc5 commented

I think it's related to some confusion from the setup perspective. I'm not 100% sure yet - I will try to investigate.

In general HF distributes two adapters - Isomorphic and Browser.

Isomorphic adapter works by checking if there is XHR in the current environment, if yes - we use it, if not - it means we are on the server and we use http module.

Browser adapter assumes there is XHR and we are forcing to use it, because there is no other option.

What are my guesses?
From what the error looks like, I presume that Nuxt is using a browser adapter instead of a server one from our build. This is not a bug of Nuxt, rather it's the environment of the application itself or missing settings in package.json of HF. Anyway, I want to clarify this to be 100% sure how to prevent this problem for other environments which may have the same confussion over what build to pick.

Here are the lines in the HF which tells the environment which adapter we want to use - isomorphic or browser.
https://github.com/BetterTyped/hyper-fetch/blob/main/packages/core/package.json#L9-L13

prc5 commented

@johngerome Working in 3.3.2 ๐ŸŽ‰๐ŸŽ‰