middleware
Unanswered
American black bear posted this in #help-forum
American black bearOP
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export async function middleware(request: NextRequest) {
const allowedGateways = ["mp", "paypal", "stripe"];
const setup = request.nextUrl.searchParams.get('setup');
if(!setup || !allowedGateways.includes(setup)) {
return NextResponse.rewrite('/404');
}else {
return NextResponse.next();
}
}I have this simple middleware for a route.
The location is app/payments/middleware.ts
I created it there just to run it on the payments page.
But it's not running, like it doesn't exist, where am I failing?
Can I create middleware like this on pages or just at the root of the project?
2 Replies
Gharial
@American black bear Try moving middleware.ts to the root of your project, not in app/payments.
Barbary Lion
@American black bear Follow the documentation:
https://nextjs.org/docs/app/building-your-application/routing/middleware
But basically you can check for the url so it only runs on the payments page like this:
export const config = {
matcher: '/app/payments/:path*',
}
https://nextjs.org/docs/app/building-your-application/routing/middleware
But basically you can check for the url so it only runs on the payments page like this:
export const config = {
matcher: '/app/payments/:path*',
}