Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document usage of suspense option of next/dynamic #28210

Merged
merged 7 commits into from Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/advanced-features/dynamic-import.md
Expand Up @@ -156,3 +156,25 @@ function Home() {

export default Home
```

## With suspense

Option `suspense` let you to load component lazily as `React.lazy` and `<Suspense>` with React 18. Note that it only works on client side and server side will render `fallback` component yet. Fully SSR support in concurrent mode is working in progress.
huozhi marked this conversation as resolved.
Show resolved Hide resolved

```jsx
import dynamic from 'next/dynamic'

const DynamicLazyComponent = dynamic(() => import('../components/hello4'), {
suspense: true,
})

function Home() {
return (
<div>
<Suspense fallback={`loading`}>
<DynamicLazyComponent />
</Suspense>
</div>
)
}
```
13 changes: 13 additions & 0 deletions errors/invalid-dynamic-suspense.md
@@ -0,0 +1,13 @@
# Invalid Usage of `suspense` Option of `next/dynamic`
styfle marked this conversation as resolved.
Show resolved Hide resolved

#### Why This Error Occurred

`<Suspense>` is not allowed under legacy render mode without using React versions above 18.
huozhi marked this conversation as resolved.
Show resolved Hide resolved

#### Possible Ways to Fix It

Remove `suspense: true` option in `next/dynamic` usages.

### Useful Links

- [Dynamic Import Suspense Usage](https://nextjs.org/docs/advanced-features/dynamic-import#with-suspense)
2 changes: 1 addition & 1 deletion packages/next/shared/lib/dynamic.tsx
Expand Up @@ -119,7 +119,7 @@ export default function dynamic<P = {}>(
if (!process.env.__NEXT_REACT_ROOT && suspenseOptions.suspense) {
// TODO: add error doc when this feature is stable
throw new Error(
`Disallowed suspense option usage with next/dynamic in blocking mode`
`Invalid suspense option usage in next/dynamic. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense`
)
}
}
Expand Down