Development/React.js

next.config.js로 Redirects 설정

moretz0921 2022. 4. 14. 23:16

Redirects : 유저가 특정 url로 들어갈 때, 원하는 곳으로 redirect
 

설정
: redirects 함수는 비동기이며, source, destination, permanent등을 속성으로 가지는 객체 배열을 return.

module.exports = {
  async redirects() {
    return [
      {
        source: '/about',
        destination: '/',
        permanent: true,
      },
    ]
  },
}


별표(*)를 붙이면 뒤에 숫자 뿐만 아니라 모든 요소를 그대로 redirect 해준다.
 

{
    source: "/old/:path*",
    destination: "/new/:path*",
    permanent: false,
  },



속성
1) source : 들어오는 요청 경로 패턴
2) destination : redirecting 할 경로
3) permanent : true인 경우 http 코드가 308이 되고, false인 경우 http 코드가 307이 된다.
4) basePath : true인 경우 redirection 경로의 basePath가 적용되고, false인 경우 적용이 되지 않는다.
5) has : object 타입이며 header, cookie, query 매칭을 할 때 사용된다.

 

 

출처

https://intrepidgeeks.com/tutorial/nextjs-nextconfigjs

 

Next.js - next.config.js

next를 좀 더 커스텀하게 사용하기 위해 next.config.js 파일에서 기본 설정을 할 수 있다. next.config.js 는 JSON 파일이 아닌 일반 Node 모듈이고 next 서버 빌드 단계에서 사용되며 브라우저 빌드에는 포함

intrepidgeeks.com