Unverified Commit 0a916775 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Allow `"use cache"` functions in server action module (client layer) (#71511)

When importing a server action module from a client component, we've
already opted into the server layer, so defining inline `"use cache"`
annotated functions should be allowed.
parent 4796bffd
......@@ -149,30 +149,37 @@ impl<C: Comments> ServerActions<C> {
self.config.enabled,
);
if is_action_fn && !self.config.is_react_server_layer && !self.in_action_file {
HANDLER.with(|handler| {
handler
.struct_span_err(
span.unwrap_or(body.span),
"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/api-reference/functions/server-actions#with-client-components\n",
)
.emit()
});
}
if !self.config.is_react_server_layer {
if is_action_fn && !self.in_action_file {
HANDLER.with(|handler| {
handler
.struct_span_err(
span.unwrap_or(body.span),
"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\n\
Read more: https://nextjs.org/docs/app/api-reference/functions/server-actions#with-client-components\n",
)
.emit()
});
}
if cache_type.is_some()
&& !self.config.is_react_server_layer
&& self.in_cache_file.is_none()
{
HANDLER.with(|handler| {
handler
.struct_span_err(
span.unwrap_or(body.span),
"It is not allowed to define inline \"use cache\" annotated Cache \
Functions in Client Components.",
)
.emit()
});
if cache_type.is_some() && self.in_cache_file.is_none() && !self.in_action_file {
HANDLER.with(|handler| {
handler
.struct_span_err(
span.unwrap_or(body.span),
"It is not allowed to define inline \"use cache\" annotated \
functions in Client Components.\nTo use \"use cache\" functions \
in a Client Component, you can either export them from a \
separate file with \"use cache\" or \"use server\" at the top, \
or pass them down through props from a Server Component.\n",
)
.emit()
});
}
}
}
......
'use client'
export default function App() {
async function fn() {
'use cache'
}
return <div>App</div>
}
/* __next_internal_client_entry_do_not_use__ default auto */ export default function App() {
async function fn() {}
return <div>App</div>;
}
x It is not allowed to define inline "use cache" annotated functions in Client Components.
| To use "use cache" functions in a Client Component, you can either export them from a separate file with "use cache" or "use server" at the top, or pass them down through props from a Server
| Component.
|
,-[input.js:4:1]
3 | export default function App() {
4 | ,-> async function fn() {
5 | | 'use cache'
6 | `-> }
7 | return <div>App</div>
`----
'use server'
export async function bar() {
'use cache'
}
/* __next_internal_action_entry_do_not_use__ {"ac840dcaf5e8197cb02b7f3a43c119b7a770b272":"bar"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
export var bar = /*#__PURE__*/ createServerReference("ac840dcaf5e8197cb02b7f3a43c119b7a770b272", callServer, void 0, findSourceMapURL, "bar");
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