You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"use server";import{redirect}from"next/navigation";import{revalidatePath}from"next/cache";exportasyncfunctioncreatePost(id: string){try{// 데이터베이스 호출}catch(error){// 에러 핸들링}revalidatePath("/posts");// 캐싱되어있는 게시글 목록 업데이트redirect(`/post/${id}`);// 새로운 게시글 페이지로 이동}
permanentRedirect 함수
보통 영구적으로 다른 URL로 이동시키기 위해서 사용함
서버컴포넌트, 서버액션, 라우트 핸들러에서 사용이 가능함
엔티티의 표준 URL을 변경할때 등 변화 또는 이벤트 이후에 주로 사용함
예를 들어서 유저가 이름을 변경하면 업데이트된 URL로 이동시킬때 사용
"use server";import{permanentRedirect}from"next/navigation";import{revalidateTag}from"next/cache";exportasyncfunctionupdateUsername(username: string,formData: FormData){try{// DB 호출}catch(error){// 에러 핸들링}revalidateTag("username");// 모든 username에 대한 캐시를 갱신permanentRedirect(`/profile/${username}`);// 새로운 유저 페이지로 이동}
import{NextResponse,NextRequest}from"next/server";import{authenticate}from"auth-provider";exportfunctionmiddleware(request: NextRequest){constisAuthenticated=authenticate(request);// 인증된 유저라면, 다음 절차를 그대로 진행함if(isAuthenticated){returnNextResponse.next();}// 인증되지 않은 유저라면 /login 페이지로 redirect 시킴returnNextResponse.redirect(newURL("/login",request.url));}exportconstconfig={matcher: "/dashboard/:path*",};
많은 양의 리다이렉트 관리하기 (고급기능)
1000개가 넘어가는 리다이렉트를 관리하기 위해서는 커스텀 미들웨어가 필요함
배포를 다시 안해도 리다이렉트를 동적으로 처리할 수 있음
방법
NoSQL과 같은 key-value 기반 DB나 JSON 파일을 활용해서 redirect map을 구성함