[React] hosting 환경 구분

이경은·2023년 3월 14일
0

hosting 시에 app에서 현재 환경이 localhost인지 배포 환경인지 구분해서 동작해야 할 때 사용하는 방식.

const isLocalhost = Boolean(
  window.location.hostname === 'localhost' ||
  // [::1] is the IPv6 localhost address.
  window.location.hostname === '[::1]' ||
  // 127.0.0.1/8 is considered localhost for IPv4.
  window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/)
);
profile
Web Developer

0개의 댓글