Unverified Commit ad556aee authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix compilation of next/dynamic with `ssr: false` in App Router (#54411)

For the server compilation, we currently transpile the dynamic import
expression to `null` if `ssr` is disabled. However to make the Server
Actions layer work (as it can be created again from a Client Component),
we can't do that optimization.

This PR changes it to always keep that import expression when
`react_server_components` (App Router) is enabled, no matter which layer
it's on.

Closes #52672.
parent 1e3920c3
...@@ -216,7 +216,9 @@ where ...@@ -216,7 +216,9 @@ where
opts.is_server, opts.is_server,
match &opts.server_components { match &opts.server_components {
Some(config) if config.truthy() => match config { Some(config) if config.truthy() => match config {
react_server_components::Config::WithOptions(x) => x.is_server, // Always enable the Server Components mode for both
// server and client layers.
react_server_components::Config::WithOptions(_) => true,
_ => false, _ => false,
}, },
_ => false, _ => false,
......
...@@ -103,6 +103,58 @@ fn next_dynamic_fixture(input: PathBuf) { ...@@ -103,6 +103,58 @@ fn next_dynamic_fixture(input: PathBuf) {
); );
} }
#[fixture("tests/fixture/next-dynamic-app-dir/**/input.js")]
fn app_dir_next_dynamic_fixture(input: PathBuf) {
let output_dev = input.parent().unwrap().join("output-dev.js");
let output_prod = input.parent().unwrap().join("output-prod.js");
let output_server = input.parent().unwrap().join("output-server.js");
test_fixture(
syntax(),
&|_tr| {
next_dynamic(
true,
false,
true,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
},
&input,
&output_dev,
Default::default(),
);
test_fixture(
syntax(),
&|_tr| {
next_dynamic(
false,
false,
true,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
},
&input,
&output_prod,
Default::default(),
);
test_fixture(
syntax(),
&|_tr| {
next_dynamic(
false,
true,
true,
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
},
&input,
&output_server,
Default::default(),
);
}
#[fixture("tests/fixture/ssg/**/input.js")] #[fixture("tests/fixture/ssg/**/input.js")]
fn next_ssg_fixture(input: PathBuf) { fn next_ssg_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js"); let output = input.parent().unwrap().join("output.js");
......
import dynamic from 'next/dynamic'
export const NextDynamicNoSSRServerComponent = dynamic(
() => import('../text-dynamic-no-ssr-server'),
{
ssr: false,
}
)
import dynamic from 'next/dynamic';
export const NextDynamicNoSSRServerComponent = dynamic(()=>import('../text-dynamic-no-ssr-server'), {
loadableGenerated: {
modules: [
"some-file.js -> " + "../text-dynamic-no-ssr-server"
]
},
ssr: false
});
import dynamic from 'next/dynamic';
export const NextDynamicNoSSRServerComponent = dynamic(()=>import('../text-dynamic-no-ssr-server'), {
loadableGenerated: {
webpack: ()=>[
require.resolveWeak("../text-dynamic-no-ssr-server")
]
},
ssr: false
});
import dynamic from 'next/dynamic';
export const NextDynamicNoSSRServerComponent = dynamic(()=>import('../text-dynamic-no-ssr-server'), {
loadableGenerated: {
modules: [
"some-file.js -> " + "../text-dynamic-no-ssr-server"
]
},
ssr: false
});
...@@ -278,6 +278,20 @@ createNextDescribe( ...@@ -278,6 +278,20 @@ createNextDescribe(
await check(() => browser.url(), `${next.url}/client`, true, 2) await check(() => browser.url(), `${next.url}/client`, true, 2)
}) })
it('should support next/dynamic with ssr: false', async () => {
const browser = await next.browser('/dynamic-csr')
await check(() => {
return browser.elementByCss('button').text()
}, '0')
await browser.elementByCss('button').click()
await check(() => {
return browser.elementByCss('button').text()
}, '1')
})
if (isNextStart) { if (isNextStart) {
it('should not expose action content in sourcemaps', async () => { it('should not expose action content in sourcemaps', async () => {
const sourcemap = ( const sourcemap = (
......
'use client'
import { inc } from '../client/actions'
import { useState } from 'react'
export function CSR() {
const [count, setCount] = useState(0)
return (
<button onClick={async () => setCount(await inc(count))}>{count}</button>
)
}
'use client'
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('./csr').then((mod) => mod.CSR), {
ssr: false,
})
export default function Client() {
return (
<div>
<DynamicComponent />
</div>
)
}
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