Next.js Discord

Discord Forum

Middleware issue

Unanswered
Black carp posted this in #help-forum
Open in Discord
Black carpOP
I'm using clerk in my project where I have one middleware.ts file which checks the auth , now I want to include next middleware inside that file to check route param how can i do it ?

18 Replies

Request.nexturl.search
In the middleware
const noAuthRoutes = portalAuth_Config.NoAuthRoutes;
    const views = portalAuth_Config.Views;
    const referringPath = request.headers.get('referer');
    const requestDevKey = request.headers.get('devkey');
    const serverAction = request.headers.get('next-action');
    const requestedPath = request.nextUrl.pathname;
    const requestedPathParameters = request.nextUrl.search;
Black carpOP
when I run database query in middleware it gives
any solution for this

https://github.com/vercel/next.js/discussions/50177
tried but didnt resolved
This is a very different question but middleware is edge computing you can’t run non edge functions there you need to fetch to an api endpoint.
@Jboncz This is a very different question but middleware is edge computing you can’t run non edge functions there you need to fetch to an api endpoint.
Black carpOP
So like I have to separate my database query into API folder and fetch that inside middleware?
Yes. Middleware runs on the edge you only have access to edge functions.
@Black carp So like I have to separate my database query into API folder and fetch that inside middleware?
if your database also doesn't run on the edge I don't recommend making queries from the middleware, otherwise the roundtrip will make all the requests to the app very slow. my recommendation is to do basic auth checks on the middleware then full auth check on resources request
this sounds like it should be done at route level, the middleware is mostly for lightweight work for multiple routes
@Rafael Almeida this sounds like it should be done at route level, the middleware is mostly for lightweight work for multiple routes
Black carpOP
if user search short link I have to check the url parameters inside middleware isn't it?
I am not sure what you mean
Black carpOP
like ,

localhost:300/asdf

trying to get 'asdf ' from search parameter and matching it with my database shortcode
this is code specific to this route endpoint, I am not sure why you would want to do that on middleware
you can allow any "code" (like asdf in your example) as a route using a dynamic route: https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes