turbopackLazyDynamicImportsSSRExperimental
The experimental.turbopackLazyDynamicImportsSSR option defers compiling dynamic imports until server rendering first reaches them during next dev.
next.config.ts
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
turbopackLazyDynamicImportsSSR: true,
},
}
export default nextConfignext.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
turbopackLazyDynamicImportsSSR: true,
},
}
module.exports = nextConfigHow it works
Client Components can use lazy to load a component dynamically. With turbopackLazyDynamicImportsSSR enabled, Turbopack compiles the imported component when a server render first reaches it. A target that is not rendered remains uncompiled.
In the following example, Turbopack compiles Preview when showPreview is true during server rendering:
app/preview.tsx
'use client'
import { lazy } from 'react'
const Preview = lazy(() => import('./preview-panel'))
export function MaybePreview({ showPreview }: { showPreview: boolean }) {
return showPreview ? <Preview /> : null
}app/preview.js
'use client'
import { lazy } from 'react'
const Preview = lazy(() => import('./preview-panel'))
export function MaybePreview({ showPreview }) {
return showPreview ? <Preview /> : null
}Good to know:
- The option defaults to
falseand only affects the Node.js runtime with Turbopack duringnext dev. It does not change production builds.- To defer client-side dynamic imports until the browser requests them, use
turbopackLazyDynamicImports.
Version History
| Version | Changes |
|---|---|
v16.4.0 | experimental.turbopackLazyDynamicImportsSSR configuration option added. |