vercel/next.js

In Next.js version 15.1.0, the middleware is not working. I placed the middleware file under the src directory, at the same level as app.

Alndaly opened this issue · 2 comments

Link to the code that reproduces this issue

https://github.com/Alndaly/qiyue-web

To Reproduce

  1. pnpm i
  2. start the application (pnpm dev)
  3. Open the browser and go to http://localhost:3000/
  4. The problem will happen

Current vs. Expected behavior

...
export function middleware(request: NextRequest) {
    console.log('test')
    // If the user is authenticated, continue as normal
    if (auth(request)) {
        if (request.nextUrl.pathname === '/') {
            return NextResponse.redirect(new URL('/dashboard', request.url))
        }
        return NextResponse.next()
    }
    // Redirect to login page if not authenticated and the destination page is not the sign page
    return NextResponse.redirect(new URL('/login', request.url))
}
...

In terminal, it should console test, but it console nothing, and the website should be redirected to login page, it's also failed..

Provide environment information

Operating System: 
Platform: darwin 
Arch: arm64 
Version: Darwin 

Relevant Packages: 
next: 15.1.0

Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

Hi,

You have an issue with your matcher, you have:

 '/((?!api|_next/static|_next/image|favicon.ico|serviceWorker).*)/',

But I think, it should be:

   '/((?!api|_next/static|_next/image|favicon.ico|serviceWorker).*)', // without the last trailing slash

Otherwise, you are rejecting / itself from being matched.