Next.js - middleware

김종민·2023년 3월 24일
0

Next.js에서는 express에서와 같이 middleware를 사용가능하다.

만약 전역 middleware를 사용하고 싶다면 page폴더에 middleware.ts파일을 작성하면된다.

// middleware.ts
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

// 아래의 경로에 해당하는경우 /about-2로 강제로 이동시킨다.
export function middleware(request: NextRequest) {
  return NextResponse.redirect(new URL('/about-2', request.url))
}

// 특정 경로에서만 사용하고싶은경우
export const config = {
  matcher: '/about/:path*', 혹은
// matcher:['/about/:path*', '/dashboard/:path*'],
}

공식문서 URL

profile
개발을 합시다 :)

0개의 댓글