Cache doesn't work - cache(getItem( ... )) runs multiple times! ⚠❓
Answered
kyzo posted this in #help-forum
kyzoOP
Hi everyone!
I'm struggling a lot with understanding how the caching works. For simplicity, here's my app structure:
/app
└── /[user_profile_id]/
├── layout.tsx
├── page.tsx
└── /onboarding
└── page.tsx
/supabase
└── getItem.ts
As you can see, I have the getItem function which pings my Supabase Database and returns value for the Item.
This getItem() function is used in all three server components: layout, page and onboardin/page.
the getItem.ts looks like this:
export const revalidate = 3600
export const getItem = cache(async (itemId: string) => {
(...)
const {data} = supabase.select(*)...
}
So, as you can see, the revalidation is set to 3600 and the whole function is wrapped in the cache()
However, even though it's set like this, accordingly to the official documentation: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating, the function getItem runs two times (for layout and for page) - and then third time for /onboarding as well.
What I need to do, is to make sure that the this Item will be fetched from the server only once, to make user's experience much faster and reduce the number of redundant database calls.
How do I do that? What am I missing here?
I'm struggling a lot with understanding how the caching works. For simplicity, here's my app structure:
/app
└── /[user_profile_id]/
├── layout.tsx
├── page.tsx
└── /onboarding
└── page.tsx
/supabase
└── getItem.ts
As you can see, I have the getItem function which pings my Supabase Database and returns value for the Item.
This getItem() function is used in all three server components: layout, page and onboardin/page.
the getItem.ts looks like this:
export const revalidate = 3600
export const getItem = cache(async (itemId: string) => {
(...)
const {data} = supabase.select(*)...
}
So, as you can see, the revalidation is set to 3600 and the whole function is wrapped in the cache()
However, even though it's set like this, accordingly to the official documentation: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating, the function getItem runs two times (for layout and for page) - and then third time for /onboarding as well.
What I need to do, is to make sure that the this Item will be fetched from the server only once, to make user's experience much faster and reduce the number of redundant database calls.
How do I do that? What am I missing here?
Answered by DirtyCajunRice | AppDir
this isnt a segment and revalidate has no effect here.
8 Replies
Alapaha Blue Blood Bulldog
did u find a solution to this ?
Blanc de Hotot
The Fetch API already has built-in caching, so you don't need to implement that. By the way, you can't cache things and use 'revalidate' at the same time.
Btw using cache from react just does cache for each request (so not very useful unless you do the same req for the same render) - so this is a valid use I belive
this isnt a segment and revalidate has no effect here.
Answer
@DirtyCajunRice | AppDir this isnt a segment and revalidate has no effect here.
California pilchard
are you sure??
@California pilchard are you sure??
the
exporting revalidate outside special files like page.tsx has no effect
revalidate option is set to 3600 in the Layout and Page segments. they do not export revalidate in the same file utils.ts.exporting revalidate outside special files like page.tsx has no effect
@joulev the `revalidate` option is set to `3600` **in the Layout and Page segments**. they do not export revalidate in the same file utils.ts.
exporting revalidate outside special files like page.tsx has no effect
California pilchard
True that, and also if the fucntion is being called multiple times doesn't mean it makes a request to the db multiple times. I came accross this post because the fetching function I created was being called many times
@California pilchard True that, and also if the fucntion is being called multiple times doesn't mean it makes a request to the db multiple times. I came accross this post because the fetching function I created was being called many times
If your arguments are not primitives (ex. objects, functions, arrays), ensure you’re passing the same object reference.
https://react.dev/reference/react/cache#troubleshooting
https://react.dev/reference/react/cache#troubleshooting