Next.js Discord

Discord Forum

Route Handlers

Answered
Orinoco Crocodile posted this in #help-forum
Open in Discord
Orinoco CrocodileOP
If you have a client component which fetches to a routehandler, and that routehandler then fetches to an offsite API, is this what happens:
- The client (as in, javascript code downloaded by the visitor) makes a request to the nextjs backend (routehandler)
- The nextjs backend requests to the offsite API
- The offsite api responds
- The nextjs backend responds

So the example here: https://nextjs.org/docs/app/building-your-application/routing/route-handlers#caching is actually like an API, which wraps another API (at mongodb)? Shouldn't this kind of thing be discouraged as it's more traffic?

Obviously you could write the whole stack in NextJS - as it's a full stack framework - and this wouldn't need to use an offsite API.
And alternatively, you could use a library (e.g. react-query). Or even turn off caching and handle it yourself (reinvent the wheel 😬 ).
Answered by European sprat
sure, there could be more efficiency if you make a POST request to your route handler and the route handler then interacts directly with your own database but for many things, it will need to go to a 3rd party API
View full answer

16 Replies

Orinoco CrocodileOP
^ oh this is really cool lol. also pls ping me if you respond in case i don't hear!
European sprat
What is your question? If you want to use route handlers to interface with a 3rd party API you can do that.
Orinoco CrocodileOP
ah like the part with the bullet points
i was asking if that's correct
European sprat
what you describe is what happens
Orinoco CrocodileOP
ty, sorry if i phrased it awkwardly haha. surely you should try and avoid using route handlers with a 3rd party API because of increased traffic?
like it would be better to use a nextjs backend or cache the 3rd party api requests directly
European sprat
it depends on what the 3rd party API is doing
it's a valid and standard pattern for interacting with third party APIs
Orinoco CrocodileOP
oh hang on, so nextjs caches requests to the routehandler (and has features for revalidate), so GET requests would skip both of those requests wouldnt they
if the cache is still valid that is
European sprat
it depends on how you have configured your route handlers and what functions they use (if they use the request object, cookies() or headers() they are dynamic and not cached)
Orinoco CrocodileOP
right, i'm just asking like... in a general use case, you'd want to avoid making two requests whenever you want to fetch some data. some GET reqs are cached (among other things) so it's not too much of a problem, but if you're writing lots of post requests that go to the route handler and which fetches from a 3rd party api, am I correct in saying this would lose some efficiency?
apologies i might be asking the obvious i just want to make sure i'm understanding it all right
European sprat
sure, there could be more efficiency if you make a POST request to your route handler and the route handler then interacts directly with your own database but for many things, it will need to go to a 3rd party API
Answer
Orinoco CrocodileOP
gotchaa, ty for the help. this was more a discussion than a singular question ig so i'll jsut mark your last response as the answer!