Introduction
In modern web development, making HTTP requests is a fundamental task. Whether you're fetching data from a server, submitti...
For further actions, you may consider blocking this person and/or reporting abuse
Hmmm, so let's see about this: The current
axiosNPM package weighs 2MB. In exchange for adding a significant chunk of those 2MB, we get things like errors on HTTP non-OK responses, which is bad for me (and I bet others) because, for example, I want to collect the body respones for HTTP 400 responses to provide the feedback to users. HTTP response 401 is another example: I need it in order to redirect the user to a login screen.Sure, I can always do a
catch. But, withfetch, which costs 0 bytes and is also available in Node, I don't need thecatch. I merely need to do aswitchon thestatusCodeproperty, or anifon theokproperty.Now, interceptors. Do I download 2MB to get a JSON token added to the header? Or do I do this?
"But then you'll have to do it for every data-retrieving function you have!" Eh, nope. Basic programming principles applied, I just create a
myFetchfunction, a wrapper of the stockfetchfunction that injects the token every time, with just a couple hundred bytes. Response interception? Same thing.So, is really
axiosa necessary, or even attractive enough package? Not all. Not for me, anyway. It adds very little value, IMHO.When
axioswas first being written fetch wasn't in the spec and then took a while to be everywhere. We had to use XMLHttpRequest everywhere and that really wasn't a nice experience.Axiosthese days just usesfetchdirectly, though does still have XMLHttpRequest as a fallback.All this to say I understand why
axiosexists and when people like something it tends to survive a long time even if it's not really needed anymore. I personally always usefetchdirectly.Likewise, but I use nextjs, so I get all benefits of axios without even using it.
Why isn't axios needed for nextjs?
We've been using axios for all our API endpoints but all these comments have almost convinced me to change to fetch 😅 We're building a social network and performance really matters
i'm switching to fetch right away
Hello ! Don't hesitate to put colors on your
codeblocklike this example for have to have a better understanding of your code 😎I really enjoy fetch. I removed Axios from my latest React project. I always prefer to depend as little as possible on third party packages.
I just learned the conversion from fetch to axios and changed some of the routes in my project to
axios - async and await
and here everyone says fetch is better
Great article!
Thank you for the information.
Dumb article. Best practice in the serverless and especially Edge runtime world, is just stick with web standard fetch(). Axios is a dinosaur and keep making breaking changes, just move off.
great information.
Thanks for the information.
Intestingly, I wrote similar article today :)
dev.to/rahulvijayvergiya/fetch-vs-...
Most times, I feel axios is overkill in most cases. Fetch serves fine
Excellent
Once again, Nextjs is very kind with us. We can achieve all the benefits of axios even using fetch API in Nextjs. Love You Nextjs. Nice article by the way.
You don't get that for free. Next is overwriting the native fetch with its own implementation, which, while likely smaller than Axios, is still part of the relatively large bundle size you will have in any Next app.
It would probably be better than trying to roll your own with React and Axios though!
Code can be enclosed within triple backticks to enable syntax highlighting in Dev.to
I suggest you to try npmjs.com/package/ky/v/0.9.0. It's way smaller than axios, but still covers the basics.
very nice mani sir