#Hey everyone, I wrapped up work on a very small...
1 messages · Page 1 of 1 (latest)
The one thing i don't like about the fetch API is that it requires you to explicitly specify the response type being returned from the server using methods like:
res.blob()
res.json()
res.text()
In contrast, Axios simplifies this process by providing a more straightforward res.data property.
Some comments on the reasoning in the article
Typically, it is discouraged to raise an error unless the circumstances are unrecoverable or so critical that they significantly disrupt the ongoing operation of the application
As you mentioned, this is subjective, but I would argue that most people will consider HTTP 4xx as an error. In fact, it is fairly common to see people doing this:
fetch()
.then(response => {
if(!response.ok) throw response;
// normal handling
})
.catch(error => {})
This is just something Axios automated by default while fetch is more "relaxed" on that aspect. To be fair, Axios does allow users to customize this behavior via validateStatus (which can be one-off or global!!), and that is simply not an option for fetch.
Incompatibilities with Next.js
IMO, I don't think this should be a reason at all. Axios merely doesn't work too well with Next.js AT THIS MOMENT, while migrating/adapting another API is a long-term decision making. The same applies to other millions of ways of fetching data, including using a client library or directly accessing DB.
This is especially important when we know unstable_cache is a thing and that shows the direction of where App Router is going.
TBH, I disagree with Vercel current approach to data caching. It doesn't make sense to monkey patch a native API and make it the main way of doing things. It would make much more sense if that's a syntactic sugar (hence optional) and has a dedicated API, i.e. unstable_cache
I guess Vercel realized that's not the best approach, hence they stopped doing something similar when introducing NextRequest and NextResponse.
The rest are pretty well written. TBH, if I have to choose today I would use fetch as the default option without a doubt, while Axios does serve relatively niche use cases better. Mainly because fetch is a native API that's now widely supported.