How to fetch API data from Axios inside the getServerSideProps function in NextJS?
Do not use Axios. Just use fetch()
.
Next.js polyfills fetch()
by default on both the client and server, so you can just use it:
In addition to
fetch()
on the client-side, Next.js polyfillsfetch()
in the Node.js environment. You can usefetch()
in your server code (such asgetStaticProps
/getServerSideProps
) without using polyfills such asisomorphic-unfetch
ornode-fetch
.