Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Currently, `next/dynamic` will opt-in to `React.lazy` if `{ suspense: true }` is used. And React 18 will always resolve the `Suspense` boundary on the server-side, effectively ignoring the `ssr` option. The PR fixes #39609 by showing a warning message when `{ suspense: true, ssr: false }` is detected. The error documentation and the corresponding test case has also been updated. In the future, Next.js could implement a custom version of `React.lazy` that could suspense without executing the lazy-loaded component on the server-side. cc @huozhi ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Errors have helpful link attached, see `contributing.md`
- Loading branch information
Showing
6 changed files
with
108 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
test/integration/next-dynamic-with-suspense/pages/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Suspense } from 'react' | ||
import dynamic from 'next/dynamic' | ||
|
||
const Thing = dynamic(() => import('./thing'), { | ||
ssr: false, | ||
suspense: true, | ||
loading: () => 'Loading...', | ||
}) | ||
|
||
export default function IndexPage() { | ||
return ( | ||
<div> | ||
<p>Next.js Example</p> | ||
<Suspense fallback="Loading..."> | ||
<Thing /> | ||
</Suspense> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Thing() { | ||
return 'Thing' | ||
} |
41 changes: 41 additions & 0 deletions
41
test/integration/next-dynamic-with-suspense/test/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* eslint-env jest */ | ||
|
||
import webdriver from 'next-webdriver' | ||
import { join } from 'path' | ||
import { | ||
renderViaHTTP, | ||
findPort, | ||
launchApp, | ||
killApp, | ||
hasRedbox, | ||
} from 'next-test-utils' | ||
|
||
let app | ||
let appPort: number | ||
const appDir = join(__dirname, '../') | ||
|
||
describe('next/dynamic with suspense', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
it('should render server-side', async () => { | ||
const html = await renderViaHTTP(appPort, '/') | ||
expect(html).toContain('Next.js Example') | ||
expect(html).toContain('Thing') | ||
}) | ||
|
||
it('should render client-side', async () => { | ||
const browser = await webdriver(appPort, '/') | ||
const warnings = (await browser.log()).map((log) => log.message).join('\n') | ||
|
||
expect(await hasRedbox(browser)).toBe(false) | ||
expect(warnings).toMatch( | ||
/"ssr: false" is ignored by next\/dynamic because you can not enable "suspense" while disabling "ssr" at the same time/gim | ||
) | ||
|
||
await browser.close() | ||
}) | ||
}) |