Is there any provided API to check a next route matches?
Unanswered
Large garden bumble bee posted this in #help-forum
Large garden bumble beeOP
Here's my ideal goal. I have a
Is something like this doable?
middleware.ts, and given the request, I would like to check that the route matches, using some apifunction 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 bee 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
typescript
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?
yes, you need to run your middleware for all matches (all path, except some system paths) and then inside your middleware, you create a fetch request, await the result and check with the result if you want to do a specific action
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?