The Next.js App Router introduces a new, simplified data fetching system built on React and the Web platform. This page will go through the fundamental concepts and patterns to help you manage your data's lifecycle.
Good to know: Previous Next.js data fetching methods such as
getServerSideProps
,getStaticProps
, andgetInitialProps
are not supported in the newapp
directory.
Here's a quick overview of the recommendations on this page:
fetch()
APIThe new data fetching system is built on top of the native fetch()
Web API and makes use of async
/await
in Server Components.
fetch
to provide automatic request deduping.fetch
options object to allow each request to set its own caching and revalidating rules.Learn how to use fetch
in Next.js.
Whenever possible, we recommend fetching data inside Server Components. Server Components always fetch data on the server. This allows you to:
Learn more about Client and Server Components.
Good to know: It's still possible to fetch data client-side. We recommend using a third-party library such as SWR or React Query with Client components. In the future, it'll also be possible to fetch data in Client Components using React's
use()
hook.
In this new model, you can fetch data inside layouts, pages, and components. Data fetching is also compatible with Streaming and Suspense.
Good to know: For layouts, it's not possible to pass data between a parent layout and its children. We recommend fetching data directly inside the layout that needs it, even if you're requesting the same data multiple times in a route. Behind the scenes, React and Next.js will cache and dedupe requests to avoid the same data being fetched more than once.
When fetching data inside components, you need to be aware of two data fetching patterns: Parallel and Sequential.
Learn how to implement parallel and sequential data fetching.
fetch()
Request DedupingIf you need to fetch the same data (e.g. current user) in multiple components in a tree, Next.js will automatically cache fetch
requests (GET
) that have the same input in a temporary cache. This optimization prevents the same data from being fetched more than once during a rendering pass.
fetch
requests made in Layouts, Pages, Server Components, generateMetadata
and generateStaticParams
.Good to know:
POST
requests are not automatically deduplicated. Learn more about caching.- If you're unable to use
fetch
, React provides acache
function to allow you to manually cache data for the duration of the request.
There are two types of data: Static and Dynamic.
By default, Next.js automatically does static fetches. This means that the data will be fetched at build time, cached, and reused on each request. As a developer, you have control over how the static data is cached and revalidated.
There are two benefits to using static data:
However, if your data is personalized to the user or you want to always fetch the latest data, you can mark requests as dynamic and fetch data on each request without caching.
Learn how to do Static and Dynamic data fetching.
Caching is the process of storing data in a location (e.g. Content Delivery Network) so doesn't need to be re-fetched from the original source on each request.
The Next.js Cache is a persistent HTTP cache that can be globally distributed. This means the cache can scale automatically and be shared across multiple regions depending on your platform (e.g. Vercel).
Next.js extends the options object of the fetch()
function to allow each request on the server to set its own persistent caching behavior. Together with component-level data fetching, this allows you to configure caching within your application code directly where the data is being used.
During server rendering, when Next.js comes across a fetch, it will check the cache to see if the data is already available. If it is, it will return the cached data. If not, it will fetch and store data for future requests.
Good to know: If you're unable to use
fetch
, React provides acache
function to allow you to manually cache data for the duration of the request.
Learn more about caching in Next.js.
Revalidation is the process of purging the cache and re-fetching the latest data. This is useful when your data changes and you want to ensure your application shows the latest version without having to rebuild your entire application.
Next.js provides two types of revalidation:
Streaming and Suspense are new React features that allow you to progressively render and incrementally stream rendered units of the UI to the client.
With Server Components and nested layouts, you're able to instantly render parts of the page that do not specifically require data, and show a loading state for parts of the page that are fetching data. This means the user does not have to wait for the entire page to load before they can start interacting with it.
To learn more about Streaming and Suspense, see the Loading UI and Streaming and Suspense pages.