返回 AiToEarn
bundle-dynamic-imports.md
1 ---
2 title: Dynamic Imports for Heavy Components
3 impact: CRITICAL
4 impactDescription: directly affects TTI and LCP
5 tags: bundle, dynamic-import, code-splitting, next-dynamic
6 ---
7
8 ## Dynamic Imports for Heavy Components
9
10 Use `next/dynamic` to lazy-load large components not needed on initial render.
11
12 **Incorrect (Monaco bundles with main chunk ~300KB):**
13
14 ```tsx
15 import { MonacoEditor } from './monaco-editor'
16
17 function CodePanel({ code }: { code: string }) {
18 return <MonacoEditor value={code} />
19 }
20 ```
21
22 **Correct (Monaco loads on demand):**
23
24 ```tsx
25 import dynamic from 'next/dynamic'
26
27 const MonacoEditor = dynamic(
28 () => import('./monaco-editor').then(m => m.MonacoEditor),
29 { ssr: false }
30 )
31
32 function CodePanel({ code }: { code: string }) {
33 return <MonacoEditor value={code} />
34 }
35 ```
36
36 lines MARKDOWN