| 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 } |
| 16 | |
| 17 | function CodePanel({ code |
| 18 | return <MonacoEditor |
| 19 | } |
| 20 | ``` |
| 21 | |
| 22 | **Correct (Monaco loads on demand):** |
| 23 | |
| 24 | ```tsx |
| 25 | import dynamic from "next/dynamic |
| 26 | |
| 27 | const MonacoEditor = dynamic |
| 28 | ssr: false, |
| 29 | }); |
| 30 | |
| 31 | function CodePanel({ code |
| 32 | return <MonacoEditor |
| 33 | } |
| 34 | ``` |