Does app dir dedup only the fetch call?
Unanswered
Philippine Crocodile posted this in #help-forum
Philippine CrocodileOP
I am working with an old third-party API, which, since it is written in xml, requires every fetch to be validated and parsed. They payload is relatively heavy at times, so the validation alone is sometimes a bit slow.
How could this be fixed if multiple child routes need the same data? I can't fetch in the layout because then I would not be able to access the data in the page component.
will wrapping the entire fetch in react's cache() do anything?
How could this be fixed if multiple child routes need the same data? I can't fetch in the layout because then I would not be able to access the data in the page component.
will wrapping the entire fetch in react's cache() do anything?
8 Replies
yeah you need to use
cache so it works with anything that isn't using fetch: https://nextjs.org/docs/app/building-your-application/data-fetching/caching#react-cache@Rafael Almeida yeah you need to use `cache` so it works with anything that isn't using `fetch`: <https://nextjs.org/docs/app/building-your-application/data-fetching/caching#react-cache>
Philippine CrocodileOP
do you have any idea on how this works? isn't serverless like different instances in different regions? how do they share the cache?
the cache of both react's
cache and next's fetch are only for the request duration, so in a single request for a specific page multiple components can make request for the same endpoint and fire only a single request. the next time you need to generate the page another request will be firedthis cache doesn't live beyond the request time, so it has no problem working on serverless platforms
@Rafael Almeida the cache of both react's `cache` and next's `fetch` are only for the request duration, so in a single request for a specific page multiple components can make request for the same endpoint and fire only a single request. the next time you need to generate the page another request will be fired
Philippine CrocodileOP
so if a resource is used in routes
/141 and /141/blabla it will have to be fetched twice? shouldn't layouts solve this very problem?no, this was never the objective of layouts, this is a completely different problem
but they did introduce a cache for
fetch with a revalidation level if that's what you meanthe equivalent for anything besides
GET requests with fetch is unstable_cache but it's not ready yet, you can use it but there's no documentation