Dynamic Import

Examples

Next.js supports lazy loading external libraries with import() and React components with next/dynamic. Deferred loading helps improve the initial loading performance by decreasing the amount of JavaScript necessary to render the page. Components or libraries are only imported and included in the JavaScript bundle when they’re used.

next/dynamic is an extension of React.lazy. When used in combination with Suspense, components can delay hydration until the Suspense boundary is resolved.

Example

By using next/dynamic, the header component will not be included in the page’s initial JavaScript bundle. The page will render the Suspense fallback first, followed by the Header component when the Suspense boundary is resolved.

  1. import dynamic from 'next/dynamic'
  2. import { Suspense } from 'react'
  3. const DynamicHeader = dynamic(() => import('../components/header'), {
  4. suspense: true,
  5. })
  6. export default function Home() {
  7. return (
  8. <Suspense fallback={`Loading...`}>
  9. <DynamicHeader />
  10. </Suspense>
  11. )
  12. }

Note: In import('path/to/component'), the path must be explicitly written. It can’t be a template string nor a variable. Furthermore the import() has to be inside the dynamic() call for Next.js to be able to match webpack bundles / module ids to the specific dynamic() call and preload them before rendering. dynamic() can’t be used inside of React rendering as it needs to be marked in the top level of the module for preloading to work, similar to React.lazy.

If you are not using React 18, you can use the loading attribute in place of the Suspense fallback.

  1. const DynamicHeader = dynamic(() => import('../components/header'), {
  2. loading: () => <div>Loading...</div>,
  3. })

With named exports

To dynamically import a named export, you can return it from the Promise returned by import():

  1. // components/hello.js
  2. export function Hello() {
  3. return <p>Hello!</p>
  4. }
  5. // pages/index.js
  6. import dynamic from 'next/dynamic'
  7. const DynamicComponent = dynamic(() =>
  8. import('../components/hello').then((mod) => mod.Hello)
  9. )

With no SSR

To dynamically load a component on the client side, you can use the ssr option to disable server-rendering. This is useful if an external dependency or component relies on browser APIs like window.

  1. import dynamic from 'next/dynamic'
  2. const DynamicHeader = dynamic(() => import('../components/header'), {
  3. ssr: false,
  4. })

With external libraries

This example uses the external library fuse.js for fuzzy search. The module is only loaded in the browser after the user types in the search input.

  1. import { useState } from 'react'
  2. const names = ['Tim', 'Joe', 'Bel', 'Lee']
  3. export default function Page() {
  4. const [results, setResults] = useState()
  5. return (
  6. <div>
  7. <input
  8. type="text"
  9. placeholder="Search"
  10. onChange={async (e) => {
  11. const { value } = e.currentTarget
  12. // Dynamically load fuse.js
  13. const Fuse = (await import('fuse.js')).default
  14. const fuse = new Fuse(names)
  15. setResults(fuse.search(value))
  16. }}
  17. />
  18. <pre>Results: {JSON.stringify(results, null, 2)}</pre>
  19. </div>
  20. )
  21. }