Skip to content

Commit

Permalink
remove unstable doc
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Aug 13, 2021
1 parent 9ff8f57 commit ab14efa
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 77 deletions.
42 changes: 0 additions & 42 deletions docs/advanced-features/dynamic-import.md
Expand Up @@ -156,45 +156,3 @@ function Home() {

export default Home
```

## With suspense

In React 18, `<Suspense>` and `React.lazy` can work with SSR. So dynamic provide an option `suspense` to let you choose if you want to load a component under suspense.

```jsx
import { Suspense } from 'react'
import dynamic from 'next/dynamic'

const Profile = dynamic(() => import('./profile'), { suspense: true })

function Home() {
return (
<div>
<Suspense fallback={'loading...'}>
<Profile />
</Suspense>
</div>
)
}

export default Home
```

If you set option `suspense` to true, other options will be omitted, delegating rendering control to React `<Suspense />`.
It's similar to using `React.lazy` directly, but `next/dynamic` will support mode like CSR and SSG.

To use `suspense`, you need to enable `reactRoot` and `concurrentFeatures` in `next.config.js`.

Note: `reactRoot` will be enabled by default when using React 18.

```jsx
// next.config.js
module.exports = {
experimental: {
reactRoot: true,
concurrentFeatures: true,
},
}
```

If you only enable `reactRoot` but not `concurrentFeatures`, suspense will always render fallbacks on server side.
25 changes: 0 additions & 25 deletions errors/no-suspense-in-blocking-mode.md

This file was deleted.

2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Expand Up @@ -1185,7 +1185,7 @@ export default async function getBaseWebpackConfig(
config.reactStrictMode
),
'process.env.__NEXT_REACT_ROOT': JSON.stringify(hasReactRoot),
'process.env.__NEXT_CONCURRENT_MODE': JSON.stringify(
'process.env.__NEXT_CONCURRENT_FEATURES': JSON.stringify(
config.experimental.concurrentFeatures && hasReactRoot
),
'process.env.__NEXT_OPTIMIZE_FONTS': JSON.stringify(
Expand Down
3 changes: 1 addition & 2 deletions packages/next/server/render.tsx
Expand Up @@ -1007,8 +1007,7 @@ export async function renderToHTML(
buffers.push(chunk)
})
stream.once('end', () => {
const content = Buffer.concat(buffers).toString('utf-8')
resolve(content)
resolve(Buffer.concat(buffers).toString('utf-8'))
})

const {
Expand Down
5 changes: 3 additions & 2 deletions packages/next/shared/lib/dynamic.tsx
Expand Up @@ -114,11 +114,12 @@ export default function dynamic<P = {}>(
loadableOptions = { ...loadableOptions, ...options }

const suspenseOptions = loadableOptions as LoadableSuspenseOptions<P>
if (!process.env.__NEXT_CONCURRENT_MODE) {
if (!process.env.__NEXT_CONCURRENT_FEATURES) {
// Error if react root is not enabled and `suspense` option is set to true
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, read more: https://nextjs.org/docs/messages/page-without-valid-component.`
`Disallowed suspense option usage with next/dynamic in blocking mode`
)
}
suspenseOptions.suspense = false
Expand Down
4 changes: 0 additions & 4 deletions test/integration/react-18/app/components/bar.js
Expand Up @@ -4,10 +4,6 @@ import { useCachedPromise } from './promise-cache'

const Foo = dynamic(() => import('./foo'), {
suspense: true,
loadableGenerated: {
modules: ['./foo'],
webpack: [require.resolveWeak('./foo')],
},
})

export default function Bar() {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/react-18/test/basics.js
Expand Up @@ -19,7 +19,7 @@ export default (context) => {
expect(res2.status).toBe(200)
})

it('should not preload modules on server side', async () => {
it('should render fallback without preloads on server side', async () => {
const html = await renderViaHTTP(context.appPort, '/suspense/no-preload')
const $ = cheerio.load(html)
const nextData = JSON.parse($('#__NEXT_DATA__').text())
Expand Down

0 comments on commit ab14efa

Please sign in to comment.