next.js unstable cache not working.
Unanswered
American Chinchilla posted this in #help-forum
American ChinchillaOP
Hi, I am trying to make it so when ever I delete a product, next.js auto refreshes can calls the products again.
I know I can use react query for this, but im trying with unstable cache.
So this is my wrapper cache function:
my getProducts:
Where I am calling the function in a server component:
In my next auth file i added experimental: {
staleTimes: {
dynamic: 0,
},
},
And finally im revalidating the tag in my API route by calling
I know I can use react query for this, but im trying with unstable cache.
So this is my wrapper cache function:
export const getCachedProducts = (
userId: string,
{ limit }: { limit?: number }
) => {
const cacheFn = dbCache(productService.getProducts, {
tags: ['user-id-product'],
})
return cacheFn(userId, { limit })
}
my getProducts:
getProducts: async (id: string, {limit}:{limit?: number}) => {
const result = await db.query.ProductTable.findMany({
where: ({ userId }, { eq }) => eq(userId, id),
orderBy: ({ createdAt }, { desc }) => desc(createdAt),
limit,
})
return result
},
Where I am calling the function in a server component:
const DashboardPage = async () => {
const user = await getUser()
const products = await getCachedProducts(id,{limit:6})
...
In my next auth file i added experimental: {
staleTimes: {
dynamic: 0,
},
},
And finally im revalidating the tag in my API route by calling
revalidateTag('user-id-product')
13 Replies
American ChinchillaOP
what happens right now is the data is still being cached and not getting the new products
its not sync with the DB basically the UI
This problem or confusion was addressed in: [Need help in revalidation using API routes handlers](https://nextjs-forum.com/post/1349134799083929718)
@Losti! This problem or confusion was addressed in: [Need help in revalidation using API routes handlers](https://discord.com/channels/752553802359505017/1349134799083929718)
American ChinchillaOP
Thanks yeah so it seems like il just stick to server components
Since router.refresh wont work in my case
can you send code for
dbCache
?@Anay-208 | Ping in replies can you send code for `dbCache`?
American ChinchillaOP
Its just a wrapper for unstable cache
But no worries my code works fine now
Now that i use server actions
@American Chinchilla Now that i use server actions
Was this the message that helped with the issue? https://nextjs-forum.com/post/1349134799083929718#message-1349153733623283742, from the thread Losti linked before
I ask because
What the Next.js team recommends is either (1) fetch in Server Components and pass data down as props or (2) use Route Handlers.
I ask because
getCachedProducts
implies you’re fetching data. Server Actions are meant to be used for mutations, not for data fetching. What the Next.js team recommends is either (1) fetch in Server Components and pass data down as props or (2) use Route Handlers.
@LuisLl Was this the message that helped with the issue? https://discord.com/channels/752553802359505017/1349134799083929718/1349153733623283742, from the thread Losti linked before
I ask because `getCachedProducts` implies you’re fetching data. Server Actions are meant to be used for mutations, not for data fetching.
What the Next.js team recommends is either (1) fetch in Server Components and pass data down as props or (2) use Route Handlers.
American ChinchillaOP
oh that just a wrapper to use with unstable cache
But its fine i solved my issue, thanks
@American Chinchilla oh that just a wrapper to use with unstable cache
I mean, by is it a wrapper to use
unstable_cache
to cache a server action returning data?