Skip to content

Commit 21b6654

Browse files
authored
Do not strip loader arg in dynamic for server components (#42426)
When `ssr: false` option is presented, we stripped the loader option for `next/dynamic` call. But for react server components (both server and client ones) we're using a `React.lazy` based implementation so we shouldn't do it for them. ## Bug - [ ] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md`
1 parent 152f51c commit 21b6654

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

Diff for: packages/next-swc/crates/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ where
185185
next_dynamic::next_dynamic(
186186
opts.is_development,
187187
opts.is_server,
188+
opts.server_components.is_some(),
188189
file.name.clone(),
189190
opts.pages_dir.clone()
190191
),

Diff for: packages/next-swc/crates/core/src/next_dynamic.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ use swc_core::{
1717
pub fn next_dynamic(
1818
is_development: bool,
1919
is_server: bool,
20+
is_server_components: bool,
2021
filename: FileName,
2122
pages_dir: Option<PathBuf>,
2223
) -> impl Fold {
2324
NextDynamicPatcher {
2425
is_development,
2526
is_server,
27+
is_server_components,
2628
pages_dir,
2729
filename,
2830
dynamic_bindings: vec![],
@@ -35,6 +37,7 @@ pub fn next_dynamic(
3537
struct NextDynamicPatcher {
3638
is_development: bool,
3739
is_server: bool,
40+
is_server_components: bool,
3841
pages_dir: Option<PathBuf>,
3942
filename: FileName,
4043
dynamic_bindings: Vec<Id>,
@@ -279,9 +282,18 @@ impl Fold for NextDynamicPatcher {
279282
props.extend(options_props.iter().cloned());
280283
}
281284
}
282-
// Don't need to strip the `loader` argument if suspense is true
283-
// See https://github.com/vercel/next.js/issues/36636 for background
284-
if has_ssr_false && !has_suspense && self.is_server {
285+
286+
// Don't strip the `loader` argument if suspense is true
287+
// See https://github.com/vercel/next.js/issues/36636 for background.
288+
289+
// Also don't strip the `loader` argument for server components (both
290+
// server/client layers), since they're aliased to a
291+
// React.lazy implementation.
292+
if has_ssr_false
293+
&& !has_suspense
294+
&& self.is_server
295+
&& !self.is_server_components
296+
{
285297
expr.args[0] = Lit::Null(Null { span: DUMMY_SP }).as_arg();
286298
}
287299

Diff for: packages/next-swc/crates/core/tests/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fn next_dynamic_errors(input: PathBuf) {
4646
next_dynamic(
4747
true,
4848
false,
49+
false,
4950
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
5051
Some("/some-project/src".into()),
5152
)

Diff for: packages/next-swc/crates/core/tests/fixture.rs

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fn next_dynamic_fixture(input: PathBuf) {
4949
next_dynamic(
5050
true,
5151
false,
52+
false,
5253
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
5354
Some("/some-project/src".into()),
5455
)
@@ -61,6 +62,7 @@ fn next_dynamic_fixture(input: PathBuf) {
6162
syntax(),
6263
&|_tr| {
6364
next_dynamic(
65+
false,
6466
false,
6567
false,
6668
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
@@ -77,6 +79,7 @@ fn next_dynamic_fixture(input: PathBuf) {
7779
next_dynamic(
7880
false,
7981
true,
82+
false,
8083
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
8184
Some("/some-project/src".into()),
8285
)

Diff for: test/e2e/app-dir/app/app/dashboard/index/dynamic-imports/dynamic-server.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import dynamic from 'next/dynamic'
22

3-
const Dynamic = dynamic(() => import('../text-dynamic-server'))
3+
const Dynamic = dynamic(() => import('../text-dynamic-server'), {
4+
ssr: false,
5+
})
46

57
export function NextDynamicServerComponent() {
68
return <Dynamic />

0 commit comments

Comments
 (0)