[Next.js] router.pathname과 router.route의 차이

2020. 12. 28. 23:28카테고리 없음

Photo by Nubelson Fernandes on Unsplash

 

Next.js 에서 router의 프로퍼티 중 pathname과 route의 차이가 뭘까요?

 

알아보고자 API Document를 찾아보았지만 pathname은 있었지만 route에 대한 설명은 없었습니다.

 

라우터를 통한 페이지 이동 후 출력을 해보았을 때에 둘 다 같은 경로를 출력하는 것을 확인할 수 있었습니다.

 

혹시나해서 해당 API Document의 변경 이력을 찾아보니 route에 대한 설명이 사라졌음을 확인했습니다.

github.com/vercel/next.js/commit/18d8c90c3f9bead7ac478f6c9a32bdb5d2591832#diff-129f17bae1a499f319a70b9e5821656cbe48f7b66363d653ab2bfa18e210bdce

 

Updated docs for pathname (#9964) · vercel/next.js@18d8c90

* Updated docs for pathname * Apply suggestions from Tim Co-Authored-By: Tim Neutkens <tim@timneutkens.nl> * Removed trailing dots Co-authored-by: Tim Neutkens <tim@timneutkens...< p=""> </tim@timneutkens...<></tim@timneutkens.nl>

github.com

원래 router.route는 query를 포함한 경로를 출력하였고 router.pathname은 기존과 같이 경로만을 출력하였습니다.

 

다만 이 후에 query에 담기는 객체의 패턴이 달라지면서 route에서 query를 포함한 경로를 출력하기 어렵게 되며 pathname과 동일한 역할을 하게 된 것으로 보입니다.

 

Difference between router.pathname and router.route in nextjs 에 대한 답변
https://stackoverflow.com/a/65466725/5570106

 

Difference between router.pathname and router.route in nextjs

Basically, I want to make of NextJS router to access page url and I do this: import { useRouter } from "next/router"; const SomeComp = props => { const router = useRouter(); } How...

stackoverflow.com

 

- 2020.12.29 오후 2:12에 route와 pathname 관련 내용이 수정되었습니다.