Unverified Commit 13e769a3 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Improve error message when using per function "use server" in Client Components (#52214)

The current message isn't very clear about `"use server" function` and
`"use server" file`, and there's no link to docs to explain it further:

```
"use server" functions are not allowed in client components. 
You can import them from a "use server" file instead.
```

This PR makes it a bit more verbose:

```
It is not allowed to define inline "use server" annotated Server Actions in Client Components.
You can either mark the entire file by putting "use server" at the top, or pass them down through props from a Server Component.

Read more: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions#with-client-components
```
parent ef89890f
...@@ -22,7 +22,7 @@ use turbopack_binding::swc::core::{ ...@@ -22,7 +22,7 @@ use turbopack_binding::swc::core::{
#[serde(deny_unknown_fields, rename_all = "camelCase")] #[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Config { pub struct Config {
pub is_server: bool, pub is_server: bool,
pub enabled: bool pub enabled: bool,
} }
pub fn server_actions<C: Comments>( pub fn server_actions<C: Comments>(
...@@ -110,8 +110,7 @@ impl<C: Comments> ServerActions<C> { ...@@ -110,8 +110,7 @@ impl<C: Comments> ServerActions<C> {
handler handler
.struct_span_err( .struct_span_err(
body.span, body.span,
"\"use server\" functions are not allowed in client components. \ "It is not allowed to define inline \"use server\" annotated Server Actions in Client Components.\nTo use Server Actions in a Client Component, you can either export them from a separate file with \"use server\" at the top, or pass them down through props from a Server Component.\n\nRead more: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions#with-client-components\n",
You can import them from a \"use server\" file instead.",
) )
.emit() .emit()
}); });
......
...@@ -168,7 +168,10 @@ fn react_server_actions_server_errors(input: PathBuf) { ...@@ -168,7 +168,10 @@ fn react_server_actions_server_errors(input: PathBuf) {
), ),
server_actions( server_actions(
&FileName::Real("/app/item.js".into()), &FileName::Real("/app/item.js".into()),
server_actions::Config { is_server: true }, server_actions::Config {
is_server: true,
enabled: true
},
tr.comments.as_ref().clone(), tr.comments.as_ref().clone(),
) )
) )
...@@ -200,7 +203,10 @@ fn react_server_actions_client_errors(input: PathBuf) { ...@@ -200,7 +203,10 @@ fn react_server_actions_client_errors(input: PathBuf) {
), ),
server_actions( server_actions(
&FileName::Real("/app/item.js".into()), &FileName::Real("/app/item.js".into()),
server_actions::Config { is_server: false }, server_actions::Config {
is_server: false,
enabled: true
},
tr.comments.as_ref().clone(), tr.comments.as_ref().clone(),
) )
) )
......
x "use server" functions are not allowed in client components. You can import them from a "use server" file instead. x It is not allowed to define inline "use server" annotated Server Actions in Client Components.
| To use Server Actions in a Client Component, you can either export them from a separate file with "use server" at the top, or pass them down through props from a Server Component.
|
| Read more: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions#with-client-components
|
,-[input.js:3:1] ,-[input.js:3:1]
3 | export default function App() { 3 | export default function App() {
4 | ,-> async function fn() { 4 | ,-> async function fn() {
......
...@@ -325,7 +325,10 @@ fn server_actions_server_fixture(input: PathBuf) { ...@@ -325,7 +325,10 @@ fn server_actions_server_fixture(input: PathBuf) {
resolver(Mark::new(), Mark::new(), false), resolver(Mark::new(), Mark::new(), false),
server_actions( server_actions(
&FileName::Real("/app/item.js".into()), &FileName::Real("/app/item.js".into()),
server_actions::Config { is_server: true }, server_actions::Config {
is_server: true,
enabled: true
},
_tr.comments.as_ref().clone(), _tr.comments.as_ref().clone(),
) )
) )
...@@ -346,7 +349,10 @@ fn server_actions_client_fixture(input: PathBuf) { ...@@ -346,7 +349,10 @@ fn server_actions_client_fixture(input: PathBuf) {
resolver(Mark::new(), Mark::new(), false), resolver(Mark::new(), Mark::new(), false),
server_actions( server_actions(
&FileName::Real("/app/item.js".into()), &FileName::Real("/app/item.js".into()),
server_actions::Config { is_server: false }, server_actions::Config {
is_server: false,
enabled: true
},
_tr.comments.as_ref().clone(), _tr.comments.as_ref().clone(),
) )
) )
......
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