Can't manage to build with pre-rendering #1784
-
Hello I'm trying to build a simple preact app that sends get requests to an API on user input. I have pasted some demo code below but for my actual project I'm using debouncing and caching. (get requests aren't send as frequently). I cannot build without the "--no-prerender" argument, even after wrapping my code in "if(typeof window !== 'undefined)". Here's the full error I'm getting:
Here's the demo code I wrote to make things simple and show how my project is set up:
Everything works if I comment out the code I'm currently wrapping with "if(typeof window !== 'undefined')" Please note that I'm a beginner developer |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
If you update your There's a few different solutions you can take:
Whichever way you go, you can remove that window check around your axios code -- it's not the issue here. |
Beta Was this translation helpful? Give feedback.
If you update your
preact-cli
version, you'll see a slightly better error message (something I worked on recently), pointing out the error is in/node_modules/form-data/lib/browser.js
. To skip over the details,axios
depends on another library calledform-data
which isn't doing something right. Just the import ofaxios
is triggering this.There's a few different solutions you can take:
fetch()
API instead-
fetch()
is built into all modern browsers and should be faster & smaller thanaxios
by a considerable margin. If you are following a tutorial or something (as you say you're a beginner), this is a bit of a different interface toaxios
, so may not be desirable.