#Route Handlers

18 messages · Page 1 of 1 (latest)

wheat acorn
#

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 😬 ).

turbid tundraBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

wheat acorn
#

^ oh this is really cool lol. also pls ping me if you respond in case i don't hear!

dense creek
#

What is your question? If you want to use route handlers to interface with a 3rd party API you can do that.

wheat acorn
#

ah like the part with the bullet points

#

i was asking if that's correct

dense creek
#

what you describe is what happens

wheat acorn
#

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

dense creek
#

it depends on what the 3rd party API is doing

#

it's a valid and standard pattern for interacting with third party APIs

wheat acorn
#

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

dense creek
#

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)

wheat acorn
#

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

dense creek
#

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

wheat acorn
#

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!