next/dynamic module provides SSR-safe dynamic imports with code splitting, loading states, and client-only rendering options.
Import
Basic Usage
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.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.ssr: false:
- Server: Renders the loading component (or nothing)
- Client: Loads and renders the component after hydration
Examples
With Named Export
With Loading State
Client-Only Component
Multiple Dynamic Components
Error Handling
How It Works
Server-Side (SSR Enabled)
vinext usesReact.lazy with Suspense:
renderToReadableStream suspends until the dynamic component loads.
Client-Side
StandardReact.lazy for code splitting:
SSR: false
Server: Renders loading state or nothing Client: UsesuseEffect to detect mount, then loads the component:
Use Cases
Heavy Third-Party Libraries
Admin Panels
Modal Dialogs
Browser-Only Features
Limitations
Comparison with React.lazy
vinext’sdynamic is a thin wrapper around React.lazy + Suspense:
Source
View source code → Implementation:/home/daytona/workspace/source/packages/vinext/src/shims/dynamic.ts