What’s the Best Way to Fetch Posts and Comments Efficiently?

0
16
Asked By CreativeCoder84 On

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

Answered By WebWizard811 On

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.

Answered By TechieGuru99 On

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.

Answered By SimpleCoder On

Sounds good to me! Go ahead with your plan.

Answered By QuickFix101 On

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!

Answered By DevNinja42 On

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.

Answered By CodeMasterX On

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.