简体   繁体   中英

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?

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/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM