Skip to content

Commit 312cf23

Browse files
committed
Add docs.
1 parent 686dd4c commit 312cf23

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

readme.md

+91
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Next.js is a minimalistic framework for server-rendered React applications.
2929
- [With `<Link>`](#with-link-1)
3030
- [Imperatively](#imperatively-1)
3131
- [Custom server and routing](#custom-server-and-routing)
32+
- [Dynamic Imports](#dynamic-imports)
3233
- [Custom `<Document>`](#custom-document)
3334
- [Custom error handling](#custom-error-handling)
3435
- [Custom configuration](#custom-configuration)
@@ -569,6 +570,96 @@ Supported options:
569570

570571
Then, change your `start` script to `NODE_ENV=production node server.js`.
571572

573+
### Dynamic Import
574+
575+
<p><details>
576+
<summary><b>Examples</b></summary>
577+
<ul>
578+
<li><a href="./examples/with-dynamic-import">With Dynamic Import</a></li>
579+
</ul>
580+
</details></p>
581+
582+
Next.js supports TC39 [dynamic import proposal](https://github.com/tc39/proposal-dynamic-import) for JavaScript.
583+
With that, you could import JavaScript modules (inc. React Components) dynamically and work with them.
584+
585+
You can think dynamic imports as another way to split your code into manageable chunks.
586+
Since Next.js supports dynamic imports with SSR, you could do amazing things with it.
587+
588+
Here are a few ways to use dynamic imports.
589+
590+
#### 1. Basic Usage (Also does SSR)
591+
592+
```js
593+
import dynamic from 'next/dynamic'
594+
const DynamicComponent = dynamic(import('../components/hello'))
595+
596+
export default () => (
597+
<div>
598+
<Header />
599+
<DynamicComponent />
600+
<p>HOME PAGE is here!</p>
601+
</div>
602+
)
603+
```
604+
605+
#### 2. With Custom Loading Component
606+
607+
```js
608+
import dynamic from 'next/dynamic'
609+
const DynamicComponentWithCustomLoading = dynamic(
610+
import('../components/hello2'),
611+
{
612+
loading: () => (<p>...</p>)
613+
}
614+
)
615+
616+
export default () => (
617+
<div>
618+
<Header />
619+
<DynamicComponentWithCustomLoading />
620+
<p>HOME PAGE is here!</p>
621+
</div>
622+
)
623+
```
624+
625+
#### 3. With No SSR
626+
627+
```js
628+
import dynamic from 'next/dynamic'
629+
const DynamicComponentWithNoSSR = dynamic(
630+
import('../components/hello3'),
631+
{ ssr: false }
632+
)
633+
634+
export default () => (
635+
<div>
636+
<Header />
637+
<DynamicComponentWithNoSSR />
638+
<p>HOME PAGE is here!</p>
639+
</div>
640+
)
641+
```
642+
643+
#### 4. With [async-reactor](https://github.com/xtuc/async-reactor)
644+
645+
> SSR support is not available here
646+
647+
```js
648+
import { asyncReactor } from 'async-reactor'
649+
const DynamicComponentWithAsyncReactor = asyncReactor(async () => {
650+
const Hello4 = await import('../components/hello4')
651+
return (<Hello4 />)
652+
})
653+
654+
export default () => (
655+
<div>
656+
<Header />
657+
<DynamicComponentWithAsyncReactor />
658+
<p>HOME PAGE is here!</p>
659+
</div>
660+
)
661+
```
662+
572663
### Custom `<Document>`
573664

574665
<p><details>

0 commit comments

Comments
 (0)