Skip to main content
The next/dynamic module provides SSR-safe dynamic imports with code splitting, loading states, and client-only rendering options.

Import

Basic Usage

The component is code-split into a separate bundle and loaded on-demand.

API Reference

dynamic

() => Promise<Component>
required
Function that returns a dynamic import.
DynamicOptions
Loading and rendering options.

Options

loading

Component shown while the dynamic component is loading.
The loading component receives props:
boolean
Whether the component is currently loading.
Error | null
Error if the import failed.
boolean
Whether the loading state has been showing long enough (always true in vinext).

ssr

Whether to render the component on the server.
When ssr: false:
  • Server: Renders the loading component (or nothing)
  • Client: Loads and renders the component after hydration
Useful for components that depend on browser APIs:

Examples

With Named Export

With Loading State

Client-Only Component

Multiple Dynamic Components

Error Handling

How It Works

Server-Side (SSR Enabled)

vinext uses React.lazy with Suspense:
renderToReadableStream suspends until the dynamic component loads.

Client-Side

Standard React.lazy for code splitting:
Vite automatically creates separate chunks for dynamic imports.

SSR: false

Server: Renders loading state or nothing Client: Uses useEffect to detect mount, then loads the component:

Use Cases

Heavy Third-Party Libraries

Admin Panels

Browser-Only Features

Limitations

Top-level only: dynamic() must be called at the module level, not inside components or conditionals.
No server-side props: Dynamic components cannot receive props from getServerSideProps in the traditional sense. Pass props explicitly.
Loading flicker: With ssr: false, the client always shows the loading state first (even if hydrating), causing a brief flicker.

Comparison with React.lazy

vinext’s dynamic is a thin wrapper around React.lazy + Suspense:

Source

View source code → Implementation: /home/daytona/workspace/source/packages/vinext/src/shims/dynamic.ts