How to fetch data in Next.js
There are many ways to fetch data with Next.js. Depending on when you need the data and what you're doing with it, you have options.
Next.js allows you to mix and match Static Generation, Server-side Rendering, and Client-side Rendering to have a genuinely hybrid app.
You can continue to fetch the data client-side to react the same way you do in React.js: hooks, fetch, etc.
Check out SWR and React Query for your client-side data fetching needs.
Next.js automatically injectsfetch
into your environment. So you do not have to worry about installing libraries likeaxios
or Fetch Polyfill
For fetching data ahead of time (for prerendering Pages only) we have:
getStaticProps
getStaticPaths
getServerSideProps
You cannot use getStaticProps
, getStaticPaths
,getServerSideProps
in components or client-side data fetching.
The results of this function are saved into a JSON file and passed as props to the client's component at runtime.
Incremental Static Regeneration revalidate: 1
allows you to update existing pages by re-rendering them in the background as traffic comes in.
Now the list of blog posts will be revalidated once per second; if you add a new blog post it will be available almost immediately, without having to re-build your app or make a new deployment.
getStaticProps
with params
getStaticProps
with params
Use getStaticPaths
to return the value of the params
Use fallback: true
on your return object for getStaticPaths
if you have a big site and don't want to statically prerender all items at once, and instead opt in to render some later at runtime via server-side rendering.
getServerSideProp
getServerSideProp
getServerSideProps
will be called at runtime during every request.
Use client-side data fetching when you need data at runtime but don't need server-side rendering.
Use getServerSideProps
when you need data at runtime but do need server-side rendering.
Don't use getServerSideProps
unless necessary. Because it computes on every request, it can be slow.
Use getStaticProps
if you have pages that rely on data that is cachable and accessible at build time.
Use getStaticProps
and getStaticPaths
if you have pages that rely on data that is cachable and accessible at build time but the pages have dynamic URL params.
Last updated