Advanced Features: Dynamic Import | Next.js
컴포넌트를 사용하는 시점에 불러오는 것.
Next.js의 Image 컴포넌트, Link 컴포넌트등이 Lazy-load 해온다.
커스텀 컴포넌트들도 그게 가능하다.
next/dynamic은 React.lazy그리고 Suspense의 합성 익스텐션이고, Suspense 바운더리가 성공되기전가지 hydration을 딜레이 할 수 있다.
import dynamic from 'next/dynamic'
const DynamicHeader = dynamic(() => import('../components/header'), {
ssr: false,
})
const Button = dynamic(() => import('../../components/Button'), {
loading : () => <div>Loading.....</div>,
})
return (
<>
<h1>다이나믹 임포트</h1>
<Button>This is Button</Button>
</>
)
Component Button
이 따로 build
되어 가져온다.