Next.js Discord

Discord Forum

Newbie question about the new App Router approach to fetching data

Unanswered
Brewer's Blackbird posted this in #help-forum
Open in Discord
Brewer's BlackbirdOP
Hi all! I'm new to the forum, I really like NextJS and I've even used it in my production projects. But I used the Pages Router approach. And now I decided that it’s worth keeping up with the times and trying App Router.

I have two related questions:
I have a project where the backend developer created an endpoint for dynamic pages, where he sends header, footer, content and... meta tags for the head tag.
Reading the doc about the wonderful, declared function https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function

However, with the new App Router approach, when we no longer use getServerSideProps and do not push data deep into the entire page including components, I have a probably stupid question:
- Is it normal that I will fetch extra data into the generateMetadata function (I only need meta tags, and there’s everything there, including header, footer and content)? Or should I ask the backend developer to split this endpoint into parts? Or I can pass already fetched data into this function?

I may know the answer to this question, but I would like to make sure. I know that fetch function caches data automatically if it is a server component. Does this mean I shouldn't worry about it? And on the server, the fetch function will be called only once, and the second time it will use the data from the cache?

- The next question arises from the previous one. Previously, I used Redux on a page component to save all the data received from the getServerSideProps function so that it could be used deeper in internal components. Should I do this now, or, if the data is indeed cached in the server components, should I just call the fetch function all the time (in the inner components also)?

3 Replies

Aleutian Tern
You're correct, don't need to worry, the fetch call will be cached. The mental model to work in nextjs app dir is to call the fetch again when necessary and leave nextjs handle the deduplication using the cache.
@Aleutian Tern You're correct, don't need to worry, the fetch call will be cached. The mental model to work in nextjs app dir is to call the fetch again when necessary and leave nextjs handle the deduplication using the cache.
Brewer's BlackbirdOP
Thank you for clearing my doubts. I’ll probably continue to use redux to simply pass data inside components, but you helped me a lot on the issue of meta tags and I don’t need to ask the backend developer to split the endpoint into parts. Thank you!
Aleutian Tern
You're welcome! Use what works, but keep in mind that the RSC/App can simplify a lot of common uses of data fetching and reduce the client bundle using RSC instead of client components. So I would suggest you to try to leverage those to some extent