When do we need to fetch API on server (getServersideProps) vs client side? #16883
-
I've been using NextJS for a while, but I still get this confusion. But after that, a question pop out again in my head "when should I serve HTML with already filled data?" I think this question kind of out of NextJS scope, but I've been thinking about this lately, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
(Note that these are my opinions and others may have different situations on when to use what) If the data that is displayed to the user changes frequently, and it's important that the user see the most up-to-date data (like a dashboard), then you should use getServerSideProps (fetches the API data on each request) or send the request from the client-side. Fetching from the client vs server is a smaller distinction and the best option may vary depending on the use case. Do consider that if you use However, for "when you want to serve HTML page with already filled data", |
Beta Was this translation helpful? Give feedback.
-
thank you so much @alanqchen , I'll mark this thread as answered. I'm happy & clear enough from your explanation 🙏 |
Beta Was this translation helpful? Give feedback.
(Note that these are my opinions and others may have different situations on when to use what)
If the data that is displayed to the user changes frequently, and it's important that the user see the most up-to-date data (like a dashboard), then you should use getServerSideProps (fetches the API data on each request) or send the request from the client-side. Fetching from the client vs server is a smaller distinction and the best option may vary depending on the use case. Do consider that if you use
getServerSideProps
to fetch the API but your API server goes down, then the page will no longer be able to be served since the page will fail to build.However, for "when you want to serve HTML …