Next.js Discord

Discord Forum

Caching a fetch-request in Nextjs15 (not API)

Unanswered
Chausie posted this in #help-forum
Open in Discord
ChausieOP
Hello I wonder how i can cache a fetch-request directly called in a server-component like
`const found = prisma.find.... return {found} 
`

I know that in API calls I can use force-cache and a revalidation but If i am just calling the function like "await getFetch(); directly from a server-component how do I cache it the best way?

I mainly want to cache the data because of pagination and search results. If a user has visited first-page and navigates to page 2 the first page sohuld be cached if user revisits etc....

4 Replies

unstable_cache
though its name is unstable, its pretty stable(though deprecated in favour of dynamicIO but that's all we have for now)
@Yi Lon Ma `unstable_cache`
ChausieOP
ty I will have a look at it
const found = unstable_cache( async ( ... ) => {
const results = return await prisma.find....
return results;
})