Back to results for “Performance”
Next.js › Bundle Analysis & Code Splitting

How do you manually split a large component using dynamic imports in Next.js?

Intermediate

Answer

The next/dynamic function lets you import a component only when it is actually needed, such as when a modal opens, keeping that component's code out of the main bundle until the moment it is required, which reduces the initial page weight.
import dynamic from 'next/dynamic';

const HeavyChart = dynamic(() => import('../components/HeavyChart'), {
  loading: () => <p>Loading chart...</p>
});
Real-world example A dashboard page loads a heavy charting library only when the user clicks a button to view analytics, keeping the initial dashboard load fast for users who never open the charts.

Common follow-ups: Can you disable server side rendering for a dynamically imported component?;How do you show a custom loading state while a dynamic component loads?