NextJS middleware does not seem to be triggered

Viewed 1842

I have the middleware.js file within /myproject/pages/middleware.js:

export function middleware(request) {
  console.log(1);
  return NextResponse.redirect(new URL('/', request.url));
}

// See "Matching Paths" below to learn more
export const config = {
  matcher: ['/test'],
};

Now what I expect is when i go to the page /test then it should redirect me to /. However nothing happens and I see my standard 404 page.

Any ideas why?

NextJs version: 12.2.2

1 Answers

Latest versions of NextJS requires user to have a single middleware on the root folder.

Instead of {root}/pages/_middleware.js, try {root}/middleware.js

Related