Next.js version 12.2.5
I attempted to use a URL object for adding query parameters as mentioned here, but it did not work as expected.
Is there a method to include query parameters in a middleware that can be accessed using useRouter()
on the server side?
// middleware.ts (conceptual example)
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
return NextResponse.rewrite({
pathname: '/',
query: { requiredParam: 'something' }, // add requiredParam inside middleware
});
}
export const config = {
matcher: ['/'],
};