Hey everyone! I'm working on a forum app that separates the frontend from the backend. Right now, I've got the Post API working great, but I've just added a commenting feature and I need some advice on how to best handle fetching posts and comments. I can modify my API to return the comments along with the post data in a single fetch request, but I'm worried that the JSON might get too large and slow things down. Alternatively, I could create a separate endpoint for comments that allows for pagination, like fetching the first 10 comments with a call to /api/post/{topic}/{id}/comment/1. This would mean making 2 or more fetch requests per page for pagination. Based on your experiences, what's the most optimal way to approach this? Thanks!
6 Answers
Having separate calls for different sets of data is usually fine. If you anticipate a lot of comments, splitting them into different requests can actually help, since it allows you to render the page content without waiting for all the comments to load.
In my experience, we've handled 10+ fetch requests on a single page before without any issues. If it starts to weigh down performance, you can always revisit and optimize later.
Sounds good to me! Go ahead with your plan.
Just implement your idea and keep an eye on performance. If you find things lagging down the line, you can check your logs and address any slowdowns as they come!
You can definitely fetch comments along with posts without worrying too much about the JSON size, especially if you aren’t expecting thousands of comments just yet. Plus, if your server has compression, that can help manage the payload size. I suggest you default to fetching more comments in one go, like 50, to reduce the number of requests you make overall.
Honestly, I don't think it's a huge deal. Convenience is key here—you mentioned that fetching comments alongside posts is easier, and that's totally valid! Modern web apps handle multiple fetch requests like champs. Just make sure your error handling is on point, especially for cases when things go wrong.

Related Questions
How to Build a Custom GPT Journalist That Posts Directly to WordPress
Cloudflare Origin SSL Certificate Setup Guide
How To Effectively Monetize A Site With Ads