Next.js Discord

Discord Forum

Is there any provided API to check a next route matches?

Unanswered
Large garden bumble bee posted this in #help-forum
Open in Discord
Large garden bumble beeOP
Here's my ideal goal. I have a middleware.ts, and given the request, I would like to check that the route matches, using some api
function matches = curry(function matches(glob: string | string[], request: NextRequest) {
  return where({
    nextUrl: where({
      pathname: matches(glob),
    }),
  });
});

const middleware = cond([
  [matches("/sign-in"), deleteAuthenticationCookies],
  [matches("/admin"), redirectIfUserDoesNotHavePermission("admin", "/dashboard")],
  [matches("/user/[user_id]"), sendTelemetry],
  [T, () => NextResponse.next()],
]);

Is something like this doable?

6 Replies

Large garden bumble beeOP
right but how do you actually MATCH the routes?
@Large garden bumble bee right but how do you actually MATCH the routes?
you do something like:
if(myRoutes.includes(route)) {
  // do specific action
}
Large garden bumble beeOP
yeah but what I mean is, what if I wanna match specific syntaxes? For example,
/dashboard/*
/user/[user_id]/profile
/auth/[...authRoutes]
@Large garden bumble bee yeah but what I mean is, what if I wanna match specific syntaxes? For example, /dashboard/* /user/[user_id]/profile /auth/[...authRoutes]
then your can use stuff like startsWith or endsWith or a combination of both. Or you can use a regex. Or ...
@Large garden bumble bee solved?