nextjs에서 'window is not defined' 에러

N·2023년 2월 7일
0


원인 : Nextjs에서 SSR을 할 때 처음 렌더링하는 과정에서 window나 document 전역 객체가 존재하지 않는다.

해결방법 : typeof window !== undefined일 때 해당 소스코드가 실행되게 한다

  const saveUrl = new Promise((resolve, reject) => {
    if (typeof window !== undefined) {
      const url = window.document.location.href;
      resolve(url);
    }
  });

출처 : https://handhand.tistory.com/272

profile
web

0개의 댓글

관련 채용 정보