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

Remove extra suspense boundary for default next/dynamic #67014

Merged
merged 5 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions packages/next/src/shared/lib/dynamic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ export type DynamicOptions<P = {}> = LoadableGeneratedOptions & {
loader?: Loader<P> | LoaderMap
loadableGenerated?: LoadableGeneratedOptions
ssr?: boolean
/**
* @deprecated `suspense` prop is not required anymore
*/
suspense?: boolean
}

export type LoadableOptions<P = {}> = DynamicOptions<P>
Expand Down
8 changes: 6 additions & 2 deletions packages/next/src/shared/lib/lazy-dynamic/loadable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Suspense, lazy } from 'react'
import { Suspense, Fragment, lazy } from 'react'
import { BailoutToCSR } from './dynamic-bailout-to-csr'
import type { ComponentModule } from './types'
import { PreloadChunks } from './preload-chunks'
Expand Down Expand Up @@ -48,6 +48,10 @@ function Loadable(options: LoadableOptions) {
<Loading isLoading={true} pastDelay={true} error={null} />
) : null

// If it's non-SSR or provided a loading component, wrap it in a suspense boundary
const hasSuspenseBoundary = !opts.ssr || !!opts.loading
const Wrap = hasSuspenseBoundary ? Suspense : Fragment
const wrapProps = hasSuspenseBoundary ? { fallback: fallbackElement } : {}
const children = opts.ssr ? (
<>
{/* During SSR, we need to preload the CSS from the dynamic component to avoid flash of unstyled content */}
Expand All @@ -62,7 +66,7 @@ function Loadable(options: LoadableOptions) {
</BailoutToCSR>
)

return <Suspense fallback={fallbackElement}>{children}</Suspense>
return <Wrap {...wrapProps}>{children}</Wrap>
}

LoadableComponent.displayName = 'LoadableComponent'
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/app-dir/dynamic/app/default-loading/dynamic-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const isDevTest = false

const DynamicImportComponent = () => {
if (isDevTest && typeof window === 'undefined') {
throw new Error('This component should only be rendered on the client side')
}
return (
<div id="dynamic-component">This is a dynamically imported component</div>
)
}

export default DynamicImportComponent
26 changes: 26 additions & 0 deletions test/e2e/app-dir/dynamic/app/default-loading/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use client'

import dynamic from 'next/dynamic'

const DynamicHeader = dynamic(
() => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(import('./dynamic-component'))
}, 1000)
})
},
{
loading: () => <p>Loading...</p>,
}
)

const Page = () => {
return (
<div>
<DynamicHeader />
</div>
)
}

export default Page
7 changes: 7 additions & 0 deletions test/e2e/app-dir/dynamic/app/default/dynamic-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const DynamicImportComponent = () => {
return (
<div id="dynamic-component">This is a dynamically imported component</div>
)
}

export default DynamicImportComponent
21 changes: 21 additions & 0 deletions test/e2e/app-dir/dynamic/app/default/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use client'

import dynamic from 'next/dynamic'

const DynamicHeader = dynamic(() => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(import('./dynamic-component'))
}, 1000)
})
})

const Page = () => {
return (
<div>
<DynamicHeader />
</div>
)
}

export default Page
32 changes: 31 additions & 1 deletion test/e2e/app-dir/dynamic/dynamic.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'

describe('app dir - next/dynamic', () => {
const { next, isNextStart, skipped } = nextTestSetup({
const { next, isNextStart, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
Expand Down Expand Up @@ -57,6 +58,35 @@ describe('app dir - next/dynamic', () => {
expect($('h1').text()).toBe('hello')
})

it('should render loading by default if loading is specified and loader is slow', async () => {
const $ = await next.render$('/default-loading')

// First render in dev should show loading, production build will resolve the content.
expect($('body').text()).toContain(
isNextDev ? 'Loading...' : 'This is a dynamically imported component'
)
})

it('should not render loading by default', async () => {
const $ = await next.render$('/default')
expect($('#dynamic-component').text()).not.toContain('loading')
})

if (isNextDev) {
it('should directly raise error when dynamic component error on server', async () => {
const pagePath = 'app/default-loading/dynamic-component.js'
const page = await next.readFile(pagePath)
await next.patchFile(
pagePath,
page.replace('const isDevTest = false', 'const isDevTest = true')
)
await retry(async () => {
const { status } = await next.fetch('/default-loading')
expect(status).toBe(200)
})
})
}

describe('no SSR', () => {
it('should not render client component imported through ssr: false in client components in edge runtime', async () => {
// noSSR should not show up in html
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import React from 'react'
import dynamic from 'next/dynamic'

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

function Blue() {
return (
Expand Down
1 change: 0 additions & 1 deletion test/integration/react-current-version/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ describe('Basics', () => {
}
}
await expectToContainPreload('dynamic')
await expectToContainPreload('dynamic-suspense')
})
})
})
Expand Down
Loading