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 4 commits
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` allows you to lazy-load a component, similar to `React.lazy` and `<Suspense>` with React 18. Note that it only works on client-side or server-side with `fallback`. Full SSR support in concurrent mode is still a work-in-progress.

```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 when using React older than v18.

#### 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)
4 changes: 4 additions & 0 deletions errors/manifest.json
Expand Up @@ -151,6 +151,10 @@
"title": "invalid-assetprefix",
"path": "/errors/invalid-assetprefix.md"
},
{
"title": "invalid-dynamic-suspense",
"path": "/errors/invalid-dynamic-suspense.md"
},
{
"title": "invalid-external-rewrite",
"path": "/errors/invalid-external-rewrite.md"
Expand Down
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
4 changes: 2 additions & 2 deletions test/integration/react-18/test/index.test.js
Expand Up @@ -87,7 +87,7 @@ describe('React 18 Support', () => {
})
expect(code).toBe(1)
expect(stderr).toContain(
'Disallowed suspense option usage with next/dynamic'
'Invalid suspense option usage in next/dynamic. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense'
)
})
})
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('Basics', () => {
const html = await renderViaHTTP(appPort, '/suspense/unwrapped')
unwrappedPage.restore()
await killApp(app)
// expect(html).toContain('Disallowed suspense option usage with next/dynamic')

expect(html).toContain(
'A React component suspended while rendering, but no fallback UI was specified'
)
Expand Down