contiamo / restful-react

A consistent, declarative way of interacting with RESTful backends, featuring code-generation from Swagger and OpenAPI specs 🔥
MIT License
1.87k stars 109 forks source link

Gatsby static HTML build fails due to dependency on browser API AbortController #242

Closed mbrookson closed 4 years ago

mbrookson commented 4 years ago

The problem I've recently ported my personal site into a Gatsby application to try it out. I love restful-react, but I've hit an issue where the Gatsby static HTML build fails due to restful-react hooks depending on AbortController at the point when the hook is run, rather than when the HTTP request is made. Gatsby suggest any interactions with browser APIs be run inside a useEffect so that they run when the browser binds to React, rather than at build time. However, this is happing in a hook so I cannot move it to inside a useEffect as it breaks the rules of hooks!

Potential solution Haven't looked at the restful-react source code yet but it seems like creating an instance of AbortController is being done prematurely and should be done at the time of calling the mutate function exposed by useMutate, for example, rather than when useMutate is first run.

Alternative solutions

Example usage image

Link to Gatsby docs https://www.gatsbyjs.org/docs/debugging-html-builds/

Output of Gatsby build error

image

fabien0102 commented 4 years ago

Hi, thanks for the feedback! This should not be too hard to fix, just make sure than AbortController exists before using it should do the trick. I'm currently stuck at the hospital (16m free fall at climbing 😅) so I can't really take this issue now, but if you want to open a PR, I can review it 😉

mbrookson commented 4 years ago

Hi. I'm working on a fix but want to test locally with my Gatsby build to make sure it's working, however I'm getting an error when running yarn build or yarn watch. Any ideas?

image

fabien0102 commented 4 years ago

Never had this locally, very strange and no ideas, sorry...

mbrookson commented 4 years ago

All I did was fork master, run yarn and run yarn build 🙁

fabien0102 commented 4 years ago

Node version maybe? This is running well on my computer and Travis... So... I don't really know what to think

mbrookson commented 4 years ago

Yeah it was node version. I was running the latest version. Downgrading to LTS version resolved it.

Here's the issue if you're interested! https://github.com/nodejs/node/issues/32107

fabien0102 commented 4 years ago

Fixed! Thanks to @mbrookson 😘