Next.js Discord

Discord Forum

Is it possible to fetch data only when the data is changed?

Unanswered
Pteromalid wasp posted this in #help-forum
Open in Discord
Pteromalid waspOP
I am thinking is it possible that the data won't fetch again while the data isn't changed on sever side component? Like is there any detection that can know whether the current fetched data is the same as the data in lets say the DB or the API request?

13 Replies

how are you updating your db @Pteromalid wasp
Pteromalid waspOP
let say other user from other computer fire a submit event to update
ok so its not being handled from your own nextjs site?
Pteromalid waspOP
then when i want to jump to lets say, /post, if the other user not yet submit, it won't update fetch the api again
just use revalidatePath to revalidate the page on updating your db
Pteromalid waspOP
hmmm this is not what i want, let me describe it in more detail
Let say I have two users using two computer.
User B already in /post (data is cached) <- whenever UserB go to /post, it won't fetch again
User A update the data.
User B now going to /post <- /post fetch the api
My question is, is the revalidatePath can affect the browser in other computer? If not, is there any way to do it?
"User B now going to /post <- /post fetch the api"

This is already happening or do you want this to happen?
Pteromalid waspOP
User B is at 13:00 visit /post (/post content is cached)
User B go to /some-where-else at 13:01 then go back to /post at 13:02 (data is cached and no api is going to be fetched)
User B go to /some-where-else at 13:03 and stay there
User A update a post at 13:04
User B go to /post at 13:05 (data should be fetched again since the post has been updated)
If you're using postgres, you can use real-time tables. Supabase is particularly good for this
Pteromalid waspOP
i fetch data from api
@Pteromalid wasp i fetch data from api
If you were building this from scratch, knowing how you needed to be able to see data changes live, it would most likely push you towards Postgres/MongoDB anyway. If there is no real-time availability or pub/sub on your data storage, I suggest you actually move it to somewhere that does (assuming you're still building this product).

Otherwise, polling the data is your only real solution.