Should I Switch from Axios to Native Fetch?
Asked by
CuriousCoder123
I've been wondering if we still need to rely on libraries like Axios for HTTP requests now that the Fetch API is native in both Node.js and browsers. While Axios is great and has some useful features, I'm exploring simpler alternatives for reducing dependencies. I've come up with a few patterns to mimic common Axios functionalities with native fetch.
For instance, I created a base client pattern to handle authorization headers and base URLs. I also implemented basic retry logic and timeout handling using AbortController. These methods can provide a solid foundation, but they don't cover all features of Axios. I'd love to hear how others are managing this: Are you still using Axios, or have you made the switch to native fetch?
Answers
Iโve used Axios for a while but decided to switch to fetch for my last couple of projects. It performs well enough and helps reduce the number of dependencies I need to manage.
Reply to this answer
You really don't need to worry about security issues related to dependencies if you're keeping things simple with native fetch. However, just a heads-up about storing tokens in localStorage; it's a known risk for XSS attacks. That said, fetch is perfectly fine to use! Just be aware of the risks with sensitive data.
Reply to this answer
Iโve been moving towards native fetch for new projects. The mental overhead of maintaining an extra library like Axios just isn't worth it anymore for simple cases. Plus, wrapping fetch makes it easier to mock in tests. Just a word of caution: be careful with retry logic for non-idempotent requests! I've seen people accidentally create duplicates because they retried POST requests without checks. If you're already deep into Axios, though, migrating may not be worth the cost unless youโre doing a major overhaul.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions