Calling a Next.js API route from a middleware using fetch #34108
Replies: 2 comments 3 replies
-
Had a similar use case, it seems that currently the best practice would be to rely on a dedicated 3rd party that provides an HTTP interface, Vercel examples are all using Upstash for instance. That's probably the same problem if you want to connect to a db like Mongo (#12229). Aren't API routes also stored at the edge? So technically I guess it would work, with the limitation that you can only call an API route that is NOT protected by the same middleware, as otherwise you create an infinite loop. |
Beta Was this translation helpful? Give feedback.
-
@eric-burel . We are running Nextjs on our own infra. I have some code in my middleware that requires to encrypt something using the crypto nodejs package. Since the middleware file runs on Edge (or at least the Nextjs build says so. We get some error when building if crypto is directly used:
Now, my problem is with Docker. Is there another way to call these endpoints without using the
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have been wondering about the implications of fetching Next.js API Route from a middleware. My use case is that I want to talk to Redis (an arbitrary server instead of serverless redis like upstash) from a middleware so I'd need to run a node.js redis client (which I assume wouldn't work directly inside the middleware).
Would that be considered a bad practice due to the potential latency cost? How would vercel handle a situation like this? Would Edge functions be "close" to where next.js API routes are running? Would this add a significant latency cost to middleware?
Beta Was this translation helpful? Give feedback.
All reactions