Skip to content

Commit 2a38e86

Browse files
committed
Fix SWC dynamic transform with suspense but without ssr
1 parent 72f5c93 commit 2a38e86

File tree

6 files changed

+105
-6
lines changed

6 files changed

+105
-6
lines changed

packages/next-swc/crates/core/src/next_dynamic.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ impl Fold for NextDynamicPatcher {
226226
})))];
227227

228228
let mut has_ssr_false = false;
229+
let mut has_suspense = false;
229230

230231
if expr.args.len() == 2 {
231232
if let Expr::Object(ObjectLit {
@@ -253,21 +254,29 @@ impl Fold for NextDynamicPatcher {
253254
if let Some(Lit::Bool(Bool {
254255
value: false,
255256
span: _,
256-
})) = match &**value {
257-
Expr::Lit(lit) => Some(lit),
258-
_ => None,
259-
} {
257+
})) = value.as_lit()
258+
{
260259
has_ssr_false = true
261260
}
262261
}
262+
if sym == "suspense" {
263+
if let Some(Lit::Bool(Bool {
264+
value: true,
265+
span: _,
266+
})) = value.as_lit()
267+
{
268+
has_suspense = true
269+
}
270+
}
263271
}
264272
}
265273
}
266274
props.extend(options_props.iter().cloned());
267275
}
268276
}
269-
270-
if has_ssr_false && self.is_server {
277+
// Don't need to strip the `loader` argument if suspense is true
278+
// See https://github.com/vercel/next.js/issues/36636 for background
279+
if has_ssr_false && !has_suspense && self.is_server {
271280
expr.args[0] = Lit::Null(Null { span: DUMMY_SP }).as_arg();
272281
}
273282

packages/next-swc/crates/core/tests/fixture/next-dynamic/with-options/input.js

+5
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ const DynamicClientOnlyComponent = dynamic(
99
() => import('../components/hello'),
1010
{ ssr: false }
1111
)
12+
13+
const DynamicClientOnlyComponentWithSuspense = dynamic(
14+
() => import('../components/hello'),
15+
{ ssr: false, suspense: true }
16+
)

packages/next-swc/crates/core/tests/fixture/next-dynamic/with-options/output-dev.js

+10
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ const DynamicClientOnlyComponent = dynamic(()=>import('../components/hello')
1717
},
1818
ssr: false
1919
});
20+
const DynamicClientOnlyComponentWithSuspense = dynamic(()=>import('../components/hello')
21+
, {
22+
loadableGenerated: {
23+
modules: [
24+
"some-file.js -> " + "../components/hello"
25+
]
26+
},
27+
ssr: false,
28+
suspense: true
29+
});

packages/next-swc/crates/core/tests/fixture/next-dynamic/with-options/output-prod.js

+10
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ const DynamicClientOnlyComponent = dynamic(()=>import('../components/hello')
1717
},
1818
ssr: false
1919
});
20+
const DynamicClientOnlyComponentWithSuspense = dynamic(()=>import('../components/hello')
21+
, {
22+
loadableGenerated: {
23+
webpack: ()=>[
24+
require.resolveWeak("../components/hello")
25+
]
26+
},
27+
ssr: false,
28+
suspense: true
29+
});

packages/next-swc/crates/core/tests/fixture/next-dynamic/with-options/output-server.js

+10
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@ const DynamicClientOnlyComponent = dynamic(null, {
1616
},
1717
ssr: false
1818
});
19+
const DynamicClientOnlyComponentWithSuspense = dynamic(()=>import('../components/hello')
20+
, {
21+
loadableGenerated: {
22+
modules: [
23+
"some-file.js -> " + "../components/hello"
24+
]
25+
},
26+
ssr: false,
27+
suspense: true
28+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { createNext } from 'e2e-utils'
2+
import { NextInstance } from 'test/lib/next-modes/base'
3+
import { hasRedbox, renderViaHTTP } from 'next-test-utils'
4+
import webdriver from 'next-webdriver'
5+
6+
const suite =
7+
process.env.NEXT_TEST_REACT_VERSION === '^17' ? describe.skip : describe
8+
9+
// Skip the suspense test if react version is 17
10+
suite('dynamic with suspense', () => {
11+
let next: NextInstance
12+
13+
beforeAll(async () => {
14+
next = await createNext({
15+
files: {
16+
'pages/index.js': `
17+
import { Suspense } from "react";
18+
import dynamic from "next/dynamic";
19+
20+
const Thing = dynamic(() => import("./thing"), { ssr: false, suspense: true });
21+
22+
export default function IndexPage() {
23+
return (
24+
<div>
25+
<p>Next.js Example</p>
26+
<Suspense fallback="Loading...">
27+
<Thing />
28+
</Suspense>
29+
</div>
30+
);
31+
}
32+
`,
33+
'pages/thing.js': `
34+
export default function Thing() {
35+
return "Thing";
36+
}
37+
`,
38+
},
39+
dependencies: {},
40+
})
41+
})
42+
afterAll(() => next.destroy())
43+
44+
it('should render server-side', async () => {
45+
const html = await renderViaHTTP(next.url, '/')
46+
expect(html).toContain('Next.js Example')
47+
expect(html).toContain('Thing')
48+
})
49+
50+
it('should render client-side', async () => {
51+
const browser = await webdriver(next.url, '/')
52+
expect(await hasRedbox(browser)).toBe(false)
53+
await browser.close()
54+
})
55+
})

0 commit comments

Comments
 (0)