I'm using NextJS middleware and can get the nextUrl
object from the request, which includes things like pathname, but how do I get query string parameters from within the middleware? I can see it comes back as part of the string returned by href which I could then parse myself but I was wondering if it is returned in an object of it's own?
e.g.
export const middleware = (request) => {
const { nextUrl: { query } } = request;
...
};
where query
equals
{
param1: 'foo',
param2: 'bar',
etc.
}
nextUrl
object already includes searchParams
which is a valid URLSearchParams
instance.
E.G. usage
export function middleware(req: NextRequest) {
if(req.nextUrl.searchParams.get('flag')) {
return NextResponse.rewrite('/feature');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With