Isn't working fetching data on the Server with third-party libraries
Answered
Sun bear posted this in #help-forum
Sun bearOP
Hello there, my cache doesn't update. What issue can be here.
I've adde revalidate on
I've adde revalidate on
/app/labs/staking/page.tsx but revalidate isn't working. Screenshots added.Answered by B33fb0n3
omg, build it with cursor or something. But don't fetch 6k arrays on each request 💀
36 Replies
@Sun bear Hello there, my cache doesn't update. What issue can be here.
I've adde revalidate on `/app/labs/staking/page.tsx` but revalidate isn't working. Screenshots added.
the react cache is just for the specific request. So if you would use
If you want cache over this, use
getActiveValidators in two components, your database would only fetched once, because of react cache. But your database would fetched again on the new requestIf you want cache over this, use
unstable_cache. It's currently the only way to cache with third party libraries@B33fb0n3 the react cache is just for the specific request. So if you would use getActiveValidators in two components, your database would only fetched once, because of react cache. But your database would fetched again on the new request
If you want cache over this, use unstable_cache. It's currently the only way to cache with third party libraries
Sun bearOP
I would be really happy if you can share example!
@Sun bear I would be really happy if you can share example!
Sure, that’s an example:
import { getUser } from './data';
import { unstable_cache } from 'next/cache';
const getCachedUser = unstable_cache(
async (id) => getUser(id),
['my-app-user']
);
export default async function Component({ userID }) {
const user = await getCachedUser(userID);
...
}„getUser“ is your normal database call
@B33fb0n3 „getUser“ is your normal database call
Sun bearOP
is it good to be like database call func?
that is what I have
should it works?
Lookes good for me. Test it please
Sun bearOP
it is working, but if data changed, app doesn't fetch new one, there is still old
Yes, because it’s cached
You can invalidate the cache to get fresh data. Either though time based revalidation or by revalidating the tags.
You know how to do that?
You can invalidate the cache to get fresh data. Either though time based revalidation or by revalidating the tags.
You know how to do that?
@B33fb0n3 Yes, because it’s cached
You can invalidate the cache to get fresh data. Either though time based revalidation or by revalidating the tags.
You know how to do that?
Sun bearOP
I added revalidate
const getActiveValidatorsCached = unstable_cache(async (status) => getActiveValidators(status), ['my-app-validators'], {
revalidate: 300,
})but isn't working
@Sun bear but isn't working
try to set it to 1 and add a console.log inside your function like this:
const getActiveValidatorsCached = unstable_cache(
async (status: any) => {
console.log("fresh data")
return getActiveValidators(status)
},
['my-app-validators'],
{
revalidate: 300,
}
)@B33fb0n3 try to set it to 1 and add a console.log inside your function like this:
tsx
const getActiveValidatorsCached = unstable_cache(
async (status: any) => {
console.log("fresh data")
return getActiveValidators(status)
},
['my-app-validators'],
{
revalidate: 300,
}
)
Sun bearOP
what do you think about using swr?
that are two different things. The function using unstable cache is for serverside stuff. swr or rq is for clientside stuff
@B33fb0n3 try to set it to 1 and add a console.log inside your function like this:
tsx
const getActiveValidatorsCached = unstable_cache(
async (status: any) => {
console.log("fresh data")
return getActiveValidators(status)
},
['my-app-validators'],
{
revalidate: 300,
}
)
Sun bearOP
so I should to see fresh data in console?
maybe there is some issue with vercel?
when you set
revalidate to 1 you should be able to see the cached data for 1 second and after one second it's revalidatedSun bearOP
see in console
but looks like stuck after error
but looks like stuck after error
Sun bearOP
okay, just changed func and in console just two
fresh datawhen click update page button fresh data is activated
looks like your function is broken, because
fresh data is send. So the unstable cache function works. If you want to test the cache now change the 1 to a 10. Reload the page: you will see fresh data in the console. Reload the page again in the next 10 seconds and you won't see fresh data again. After 10 seconds, you will see it againSun bearOP
hm, looks like
unstable_cache has own api realisation, because I've got the same error when add getActiveValidators to apiSun bearOP
Error: Failed to set Next.js data cache, items over 2MB can not be cached (7637923 bytes) oh, a lot of dataoh wow, what are you fetching 😮
Sun bearOP
just array 6k + objects with data
maybe to covert to buffer array or IDK
omg, build it with cursor or something. But don't fetch 6k arrays on each request 💀
Answer
@B33fb0n3 omg, build it with cursor or something. But don't fetch 6k arrays on each request 💀
Sun bearOP
yeah, I would but I need to make functionality to search one selected object of this 6k. Thinking about make api that get key and on backbend filter all 6k and return founded object
on ui will be pagination
@Sun bear yeah, I would but I need to make functionality to search one selected object of this 6k. Thinking about make api that get key and on backbend filter all 6k and return founded object
what about using a database that handles the search? 🤔
@B33fb0n3 what about using a database that handles the search? 🤔
Sun bearOP
Keep all objects in db and make db request function to find good one?
yea, like having all the objects inside a table that is inside your database and you query your database
@B33fb0n3 yea, like having all the objects inside a table that is inside your database and you query your database
Sun bearOP
I guess I can make it! Thank you!
@Sun bear I guess I can make it! Thank you!
happy to help. Please mark solution