// app/page.tsx (index page):
export default async function Page() {
const res = await fetch("http://127.0.0.1:3000/api/products", {
next: { revalidate: 10 },
});
const data = await res.json();
return (
<div>
<h1>Home page</h1>
{data?.id}
</div>
);
}
// app/catalog/page.tsx (catalog page):
export default async function Page() {
const res = await fetch("http://127.0.0.1:3000/api/products", {
next: { revalidate: 10 },
});
const data = await res.json();
return (
<div>
<h1>Catalog page</h1>
{data?.id}
</div>
);
}
I am using Next.js v14 app router. I need to fetch data from my CMS API (http://127.0.0.1:3000/api/products) and use the data on both the index and catalog pages.
I have read docs in docs that if I need to use the same data in multiple components, I do not have to fetch data globally, nor forward props between components. Instead, I can use fetch or React cache in the component that needs the data without worrying about the performance implications of making multiple requests for the same data. This is possible because fetch requests are automatically memoized.
However, when I build my application, I notice that two fetch requests have been made, one during the index page building process and another during the catalog page building process (I guess). I would like my app to make a single fetch request to my CMS API and use the received data on several different pages.
How to achieve this?
And yes I want to keep using app router so things like getStaticProps are not relevant I guess.