Next.js Discord

Discord Forum

Need help with middleware

Answered
Nile Crocodile posted this in #help-forum
Open in Discord
Nile CrocodileOP
So I copied the middleware matcher from a Clerk.js guide since that is all I am using it for. However, I only have a few protected routes... Unfortunately (maybe), I had 255 calls to my middleware in an hour and it's basically being called on every single route it looks like. Is that expected? Just curious.
Answered by Nile Crocodile
Yea I was going to say that matcher ends up matching on everything except /_next/, /static/, /public/, /favicon.ico. So that would mean middleware.ts would run on everything else in your app. I ended up just doing just

export const config = {
  matcher: ['/admin/:path*', '/vote/:path*', '/onboarding/:path*'],
};


Since my middleware is just for my protected routes.
View full answer

23 Replies

Weevil parasitoid
Define your protected matcher paths
So no ones middleware is going to be the same.

Mine looks like

export const config = {
    matcher: '/((?!_next|static|public|favicon.ico).*)'
}
Matches: /about, /contact, /blog/post, /users/123
Doesn't match: /_next/static/image.jpg, /public/index.html, /favicon.ico
in middleware I would look at

const requestedPath = request.nextUrl.pathname;


and see what paths are triggering the middleware that you dont want and add that to your anti-matcher pattern above. Id rather accidently match on something I forgot and tell it what to ignore instead 🙂
disclaimer...

I dont use clerk, just providing general rules of thumb I follow
Nile CrocodileOP
Yea I was going to say that matcher ends up matching on everything except /_next/, /static/, /public/, /favicon.ico. So that would mean middleware.ts would run on everything else in your app. I ended up just doing just

export const config = {
  matcher: ['/admin/:path*', '/vote/:path*', '/onboarding/:path*'],
};


Since my middleware is just for my protected routes.
Answer
Yeah, exactly. I self host, in a enterprise environment so everything is protected hahah.
middleware cost is not a issue for me, since we host on our own hardware.
Nile CrocodileOP
Ah gotcha! Yea I only have a few paths protected. I host on Vercel and my usage is already blowing up with this one website... 😅
Yeah... Its hard to imagine for me..... having to deal with usage and optimizing perfectly to avoid a costly bill.... Ive done side projects in non-enterprise environments but mostly unprotected so middleware not really ever a variable.
Sounds like you solved your own issue in the end 🙂 GJ

please close the thread when you get a second, just archive it for others later, your self solution can be used as the solution 🙂
Nile CrocodileOP
Well looks like that matcher is actually broken lol
Users aren't getting directed to the /onboarding route after signing up
After signup? That wouldnt be the role of middleware though?
Show me what you got going on and ill try to help.
Nile CrocodileOP
Well after sign up they get redirected to the home page and I basically have
if (userId && !sessionClaims?.metadata?.onboardingComplete) {
    const onboardingUrl = new URL('/onboarding', req.url)
    return NextResponse.redirect(onboardingUrl)
  }


To basically say is there a userId and they haven't completed the onboarding, then redirect them to /onboarding
Well they are only redirected to the home page IF they go directly to the sign up URL. If they try to access a protected route and aren't signed in or signed up after doing so they would be redirected back to that URL they tried to visit
So because I changed it to only running on
export const config = {
  matcher: ['/admin/:path*', '/vote/:path*', '/onboarding/:path*'],
};


It wouldn't catch some of those edge cases
Your portected routes are admin and vote?
Im trying to understand all the different flows.

When Accessing protected route and going through middleware
Not signed in && onboarding not complete -> Send to login page
Signed in && onboarding not complete -> send to onboarding
signed in and onboarding complete -> send to home

those are our three scenarios right?
Nile CrocodileOP
Yea protected routes are vote and admin.

Another scenario is they access the sign up URL directly, complete sign up (currently redirected to the home page), they should be asked to complete onboarding. That is what that if(userId && !sessionClaims?.metadata?.onboardingComplete) does.
So I just need to understand what edge case isnt working properly.
Give me the play by play, remember I cant see your code base. they go where and dont get redirected properly?