[NextDoc] Advanced Features
此篇為 Next.js 官方文件 Advanced Features 之整理,非原創文章。
Dynamic Import and Code Splitting
- Dynamic Import @ next.js
- Streaming Features @ next.js
基本使用
使用 next/dynamic
可以在 Next.js 中做到 dynamic import 及 code splitting 的效果。在 React18 之後,Next.js 支援可以直接使用 lazy
來做 dynamic import。
需要留意幾點:
- import 的路徑一定要是靜態的定值,不能是 template string 或變數。
- import 的語法一定要放在
dynamic()
內
// https://nextjs.org/docs/advanced-features/dynamic-import#basic-usage
import dynamic from 'next/dynamic';
const DynamicComponent = dynamic(() => import('../components/hello'));
function Home() {
return (
<div>
<Header />
<DynamicComponent />
<p>HOME PAGE is here!</p>
</div>
);
}
export default Home;