Gatsby static HTML build fails due to dependency on browser API AbortController
See original GitHub issueThe 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
- Allow the restful-react hooks to be fired in a ‘mock’ way, which stubs them when not running in the browser. Not sure I think this is a good solution though, but would avoid any server-side issues.
- Only allow AbortController and other browser APIs to run in the browser, but still allow the fetching of data without that added functionality when running in a non-browser environment?
Example usage
Link to Gatsby docs https://www.gatsbyjs.org/docs/debugging-html-builds/
Output of Gatsby build error
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Fixed! Thanks to @mbrookson 😘
All I did was fork master, run
yarn
and runyarn build
🙁