I have some utility functions that use fetch, which I use on server. I'd like to use same functions also on client, but importing it from @remix-run/node messes it up as it doesn't exist on client (this is my assumption, it might be a xy problem). Only thing I can think of is referencing fetch function as an argument, so I can decide which one to use depending on the caller, though I feel like this is unnecessary complexity. Are there any clever way of solving this?
#Using same fetch function on both server and client
1 messages · Page 1 of 1 (latest)
fetch shouldn't need to be imported from anywhere I don't think. Its a globally available function.
fetch imported from @remix-run/node does some specific stuff, including checking if you are trying to fetch a file (hence your fs error).
So - remove your import { fetch } from "@remix-run/node" and it should just work.
If that for some reason is not the case, you could look at axios or isomorphic-fetch for your solution.
yep, fetch is globally available in the browser
aaaah, right, I felt like I was on a xy problem! yeah, as you said, I was importing it unnecessarily from @remix-run/node, remix already polyfills. thanks a lot!