Skip to content
tezvyn:

next/dynamic: Defer Loading Heavy Components

Source: nextjs.orgMediumHow cards are made

next/dynamic splits a component into its own file, loading it only when needed to shrink your initial bundle. Use it for heavy components below the fold or browser-only libraries. The footgun: forgetting a loading state causes layout shifts.

Why it exists

By default, Next.js bundles all the code needed for a page into a single JavaScript file. For complex pages with many components, this file can become very large, leading to slow initial page loads. Dynamic imports solve this by breaking the code into smaller, manageable chunks that are loaded on demand.

The mental model

next/dynamic is a form of code splitting specifically for React components. Imagine your app is a toolkit. Instead of carrying every single tool to a job, you bring a small, essential kit and have the specialized, heavy tools delivered only when you need them. This makes you faster and more efficient upfront. next/dynamic does this for your JavaScript bundles.

How it works

You wrap a standard component import in the dynamic() function from next/dynamic. Next.js's bundler sees this and creates a separate JavaScript file (a chunk) for that component and its dependencies. When your application logic requires rendering the dynamic component, Next.js fetches this chunk from the server and then mounts the component. You can provide a placeholder loading component to display while the chunk is being downloaded.

When to use it

Use next/dynamic for components that are large and not critical for the initial paint, such as a video player, a heavy data visualization library (like D3), or a rich text editor. It's also the go-to solution for integrating components that depend on browser-specific APIs (like window or document), as you can disable server-side rendering for them.

When not to use it

Avoid using it for small, simple components that are visible above the fold. The overhead of an extra network request for a tiny component can negate any benefits and might even slow down rendering. It's a tool for significant performance gains, not micro-optimizations.

One canonical example

To use a mapping library like Leaflet, which relies on the window object, you must prevent it from rendering on the server. You would import it like this: const Map = dynamic(() => import('../components/Map'), { ssr: false, loading: () => Loading map... });. This tells Next.js to never render the map on the server and to show a "Loading map..." message on the client while the component's code is fetched.

Interview question

When is next/dynamic most effectively used in a Next.js application?

  • a.To ensure all components are pre-rendered on the server for SEO benefits.
  • b.As a general optimization for every component to reduce overall application complexity.
  • c.For small, essential components that are always visible above the fold.
  • d.To load large, non-critical components or those relying on browser-specific APIs only when needed.Correct
Why?

The card states that next/dynamic is ideal for large components not critical for initial paint or for integrating components dependent on browser-specific APIs, loading them only when required. It explicitly advises against using it for small, simple components or for universal server-side rendering.

Just read this? Test yourself on what you have been reading.

Read the original → nextjs.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nextjs — each one lists the topics its interview covers.

See open roles