Results: {JSON.stringify(results, null, 2)}
Loading...
, } ) export default function Page() { return (Hello!
} ``` ```jsx filename="app/page.js" import dynamic from 'next/dynamic' const ClientComponent = dynamic(() => import('../components/hello').then((mod) => mod.Hello) ) ``` ## Magic Comments Next.js supports magic comments to control how dynamic imports are handled by the bundler. These comments work with dynamic `import()`, `require()`, `require.resolve()`, and `new Worker()` expressions. > **Good to know:** Magic comments do not work with static `import` statements (`import x from 'y'`). They only work with dynamic expressions. ### `webpackIgnore` / `turbopackIgnore` Use these comments to skip bundling a dynamic import. The import expression will be left as-is in the output, useful for runtime-only modules: ```js // Skip bundling - import happens at runtime const runtime = await import(/* webpackIgnore: true */ 'runtime-module') // Turbopack-specific variant const plugin = await import(/* turbopackIgnore: true */ pluginPath) // Also works with require const mod = require(/* webpackIgnore: true */ 'runtime-module') ``` ### `turbopackOptional` (Turbopack only) Use this comment to suppress build errors when a module might not exist. The import will still throw at runtime if the module is missing: ```js // No build error if './optional-feature' doesn't exist // Runtime will throw MODULE_NOT_FOUND if executed const feature = await import(/* turbopackOptional: true */ './optional-feature') // Also works with require const mod = require(/* turbopackOptional: true */ './optional-module') ``` This is useful for: - Conditional features that may not be installed - Plugin systems where modules are optional - Gradual migrations where some files may not exist yet > **Good to know:** `webpackOptional` is not supported. Use `turbopackOptional` instead when using Turbopack.Loading...
, }) export default function Home() { returnHello!
} // pages/index.js import dynamic from 'next/dynamic' const DynamicComponent = dynamic(() => import('../components/hello').then((mod) => mod.Hello) ) ``` ### 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`. ```jsx 'use client' import dynamic from 'next/dynamic' const DynamicHeader = dynamic(() => import('../components/header'), { ssr: false, }) ``` ### 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. ```jsx import { useState } from 'react' const names = ['Tim', 'Joe', 'Bel', 'Lee'] export default function Page() { const [results, setResults] = useState() return (Results: {JSON.stringify(results, null, 2)}