🚀   새로운 블로그로 이전했습니다.

(Next.js) nprogress

snippet: next.js
2022.12.06
1분

nprogress를 활용하여 route가 변경될시 로딩 UI를 표시할 수 있습니다.

yarn add nprogress
yarn add -D @types/nprogress

_app.tsx
useEffect(() => {
  const handleStart = (url: string) => {
    NProgress.start();
  };

  const handleStop = () => {
    NProgress.done();
  };

  router.events.on('routeChangeStart', handleStart);
  router.events.on('routeChangeComplete', handleStop);
  router.events.on('routeChangeError', handleStop);

  return () => {
    router.events.off('routeChangeStart', handleStart);
    router.events.off('routeChangeComplete', handleStop);
    router.events.off('routeChangeError', handleStop);
  };
}, [router]);
_document.tsx
<Head>
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css"
    />
</Head>

global.css
/* nprogress */
:root {
  --nprogress-color: rgb(168 85 247);
}

#nprogress .bar {
  background: var(--nprogress-color) !important;
  padding: 0 10px !important;
  height: 3px !important;
}

#nprogress .spinner .spinner-icon {
  border-top-color: var(--nprogress-color) !important;
  border-left-color: var(--nprogress-color) !important;
}

레거시 코드 참고