Dynamic imports
Next.js supports ES2020 dynamic import() for JavaScript. With it, you can import JavaScript modules dynamically and work with them. They also work with server-side rendering.
For example, you could load the module dynamically in the browser only after the user types in the search input or you just need to skip rendering some component on the server because it depends on the DOM API or client-side data.
Next.js supports dynamic imports that, when used with components, will opt out of server-side rendering. For example, if you inspect the output of the actual HTML page, you won't see the SponsoredAd component in the page source, although you can see it in the page visualization. Note: server-side rendering is false { ssr: false }
In the following example, the module ../components/hello
will be dynamically loaded by the page:
If the dynamic component is not the default export, you can use a named export too.
An optional loading
component can be added:
Last updated
Was this helpful?