Next.js Discord

Discord Forum

Is there a way to return multiple responses from the api route

Answered
Dutch Smoushond posted this in #help-forum
Open in Discord
Avatar
Dutch SmoushondOP
Consider the image below. Every variable is making a database query on a heavy database so some queries might take a lot of time while some maybe quick.

Right now we are waiting for all the queries to end and then returning all the repsonse at once this is taking around 3 to 4 minutes. Instead what I am aiming for is to return a part of the data as soon as the query is finished so i can show it on the client while also continue with the other queries. Is making multiple api routes the only way to achieve this or there is something else that I can do
Image
Answered by James4u
yeah, you need to break down your api into smaller ones - in most cases, it should be the most appropriate approach
View full answer

18 Replies

Answer
Avatar
Kromfohrländer
Avatar
@James4u yeah, you need to break down your api into smaller ones - in most cases, it should be the most appropriate approach
Avatar
Kromfohrländer
that'd work too 👍 (and is much more practical)
Avatar
Streaming is something for different purpose
Avatar
@James4u Streaming is something for different purpose
Avatar
Kromfohrländer
can it not be used here??
did I misunderstand the question 🤔
ah yeah it seems so
Avatar
at least according to OP's question
Avatar
Kromfohrländer
ignore me ^^
Avatar
@Kromfohrländer https://www.w3schools.com/html/html5_serversentevents.asp
Avatar
Dutch SmoushondOP
TIL, will look more into this later
Avatar
@Dutch Smoushond cool thanks for the info
Avatar
I believe one api route shouldn't be responsible for the multiple queries
Avatar
Dutch SmoushondOP
but @James4u is sse something you should use when you need real time response / live response
Avatar
@James4u I believe one api route shouldn't be responsible for the multiple queries
Avatar
Dutch SmoushondOP
hmm I was thinking the same actually I just got the task of optimizing things as they are slow
Avatar
@Dutch Smoushond hmm I was thinking the same actually I just got the task of optimizing things as they are slow
Avatar
yeah breaking them down should be good enough to make it faster
also utlize server components as well - fetch data and pre-render on the server
Avatar
@James4u also utlize server components as well - fetch data and pre-render on the server
Avatar
Dutch SmoushondOP
yeah that's the ideal way of fetching from an api right,

always fetch server side when you can if not then only fetch on client