[Next.js middleware] TOO_MANY_REDIRECTS

HSKwon·2024년 7월 2일
export async function middleware(request:NextRequest) {
	...
    
  if (request.nextUrl.pathname.startsWith("/admin)) {
 return NextResponse.redirect("/admin/notification")                                          
 }
}

...
...

export const config = {
  matcher: [
    "/((?!api|_next/static|_next/image|favicon.ico|main/youtube.mp4|asset).*)",
    "/admin/:path*",
  ],
};

에러 발생 원인

  • 리다이렉트 루프가 발생했기 때문에 해당 에러가 출력된것
  • 리다이렉트가 계속 발생해서 브라우저나 서버가 요청을 중단시키는 경우이다.
  • 리다이렉트 시킬 대상 경로인 "/admin/notification"도 마찬가지로 startsWith("/admin") 조건에 충족되기 때문에 루프에 빠지게 되는것이다.

해결

  • 루프에 빠지지 않도록 조건을 명확화하자.
	 if (request.nextUrl.pathname === "/admin") {
	return NextResponse.redirect("/admin/notification")
}
profile
공부한 내용이나 관심 있는 정보를 글로 정리하며 익숙하게 만들고자 합니다.

0개의 댓글