Next.js Discord

Discord Forum

Fetching data from server component

Unanswered
Holland Lop posted this in #help-forum
Open in Discord
Holland LopOP
My scenario is like I'm trying to list out online users in a page /online. Where the /online is server component(non 'use client'). External API endpoint is used to get online users using Axios. I was only getting the users active at the time of initial page load and the list was not updating even if I hit refresh page several times. Then I added export const revalidate = 60; in the server component, and all works fine.

I would like to know what had actually happened. An answer or any links would be helpful.

I was not completely aware of the reason. All I knew was the GET in the API route caches data by default. In my case also any caching happening?

4 Replies

@Holland Lop My scenario is like I'm trying to list out online users in a page `/online`. Where the `/online` is server component(non `'use client'`). External API endpoint is used to get online users using **Axios**. I was only getting the users active at the time of initial page load and the list was not updating even if I hit refresh page several times. Then I added `export const revalidate = 60;` in the server component, and all works fine. I would like to know what had actually happened. An answer or any links would be helpful. I was not completely aware of the reason. All I knew was the **GET** in the API route caches data by default. In my case also any caching happening?
you are right. Return values of a GET route handler when using the response object are cached by default.
-> this could be one reason

Another reason could be, that the browser cached the whole page (by default 30 seconds) and after this time a new request was made to the server.
-> this could be also a reason

Keep in mind, that it's not recommended to use Axios anymore: https://www.adios-axios.com/

When you just want to update the page with the live data of online users, you can do this by leveraging a clientside fetching library. This will update the content of the page whenever it's needed
Holland LopOP
Thanks for making it clear @B33fb0n3 !
Happy to help