Skip to content
Advertisement

middleware.js in nextjs doesn’t work at all

import { NextResponse } from 'next/server'

const middleware = (request) => {
  if (request.nextUrl.pathname === request.nextUrl.pathname.toLocaleLowerCase())
    return NextResponse.next()
  return NextResponse.redirect(
    `${request.nextUrl.origin}${request.nextUrl.pathname.toLocaleLowerCase()}`
  )
}

export { middleware }

(Next v.12.2.3) No matter what I code, pages/middleware.js seems not running at all!

I wanna redirect uppercase urls to lowercase urls.

what should I do?

Advertisement

Answer

The documentation says:

Create a middleware.ts (or .js) file at the same level as your pages directory

So it shouldn’t be inside pages/, but in the same directory as pages/.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement