Unverified Commit 3d23c3d9 authored by Sukka's avatar Sukka Committed by GitHub

fix(#40388): `next/dynamic` should only add default loading without suspense (#40397)

The PR fixes #40388.

Currently, `next/dynamic` will try to provide a default `loading` to the
`loadableOptions` even when `suspense` is enabled, thus triggering the
incorrect warning. The PR fixes that. The corresponding integration test
case is also updated.

cc @huozhi 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
Co-authored-by: 's avatarhuozhi <inbox@huozhi.im>
parent b09592e6
...@@ -65,7 +65,11 @@ export default function dynamic<P = {}>( ...@@ -65,7 +65,11 @@ export default function dynamic<P = {}>(
options?: DynamicOptions<P> options?: DynamicOptions<P>
): React.ComponentType<P> { ): React.ComponentType<P> {
let loadableFn: LoadableFn<P> = Loadable let loadableFn: LoadableFn<P> = Loadable
let loadableOptions: LoadableOptions<P> = {
let loadableOptions: LoadableOptions<P> = options?.suspense
? {}
: // only provide a default loading component when suspense is disabled
{
// A loading component is not required, so we default it // A loading component is not required, so we default it
loading: ({ error, isLoading, pastDelay }) => { loading: ({ error, isLoading, pastDelay }) => {
if (!pastDelay) return null if (!pastDelay) return null
...@@ -112,8 +116,8 @@ export default function dynamic<P = {}>( ...@@ -112,8 +116,8 @@ export default function dynamic<P = {}>(
) )
} }
if (process.env.NODE_ENV !== 'production') {
if (loadableOptions.suspense) { if (loadableOptions.suspense) {
if (process.env.NODE_ENV !== 'production') {
/** /**
* TODO: Currently, next/dynamic will opt-in to React.lazy if { suspense: true } is used * TODO: Currently, next/dynamic will opt-in to React.lazy if { suspense: true } is used
* React 18 will always resolve the Suspense boundary on the server-side, effectively ignoring the ssr option * React 18 will always resolve the Suspense boundary on the server-side, effectively ignoring the ssr option
...@@ -122,19 +126,20 @@ export default function dynamic<P = {}>( ...@@ -122,19 +126,20 @@ export default function dynamic<P = {}>(
* React.lazy that can suspense on the server-side while only loading the component on the client-side * React.lazy that can suspense on the server-side while only loading the component on the client-side
*/ */
if (loadableOptions.ssr === false) { if (loadableOptions.ssr === false) {
loadableOptions.ssr = true
console.warn( console.warn(
`"ssr: false" is ignored by next/dynamic because you can not enable "suspense" while disabling "ssr" at the same time. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense` `"ssr: false" is ignored by next/dynamic because you can not enable "suspense" while disabling "ssr" at the same time. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense`
) )
} }
if (loadableOptions.loading != null) { if (loadableOptions.loading != null) {
loadableOptions.loading = undefined
console.warn( console.warn(
`"loading" is ignored by next/dynamic because you have enabled "suspense". Place your loading element in your suspense boundary's "fallback" prop instead. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense` `"loading" is ignored by next/dynamic because you have enabled "suspense". Place your loading element in your suspense boundary's "fallback" prop instead. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense`
) )
} }
} }
delete loadableOptions.ssr
delete loadableOptions.loading
} }
// coming from build/babel/plugins/react-loadable-plugin.js // coming from build/babel/plugins/react-loadable-plugin.js
......
...@@ -31,6 +31,12 @@ function runTests() { ...@@ -31,6 +31,12 @@ function runTests() {
// Failure case is 'Index<!-- -->3<!-- --><!-- -->' // Failure case is 'Index<!-- -->3<!-- --><!-- -->'
expect(text).toBe('Index<!-- -->1<!-- -->2<!-- -->3<!-- -->4<!-- -->4') expect(text).toBe('Index<!-- -->1<!-- -->2<!-- -->3<!-- -->4<!-- -->4')
expect(await browser.eval('window.caughtErrors')).toBe('') expect(await browser.eval('window.caughtErrors')).toBe('')
// should not print "invalid-dynamic-suspense" warning in browser's console
const logs = (await browser.log()).map((log) => log.message).join('\n')
expect(logs).not.toContain(
'https://nextjs.org/docs/messages/invalid-dynamic-suspense'
)
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment