Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 108 additions & 132 deletions crates/next-custom-transforms/src/transforms/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,6 @@ impl<C: Comments> ServerActions<C> {
id
}

fn gen_ref_ident(&mut self) -> Atom {
let id: Atom = format!("$$RSC_SERVER_REF_{0}", self.reference_index).into();
self.reference_index += 1;
id
}

fn create_bound_action_args_array_pat(&mut self, arg_len: usize) -> Pat {
Pat::Array(ArrayPat {
span: DUMMY_SP,
Expand Down Expand Up @@ -467,16 +461,6 @@ impl<C: Comments> ServerActions<C> {
self.export_actions
.push((action_name.clone(), action_id.clone()));

let register_action_expr = bind_args_to_ref_expr(
annotate_ident_as_server_reference(action_ident.clone(), action_id.clone(), arrow.span),
ids_from_closure
.iter()
.cloned()
.map(|id| Some(id.as_arg()))
.collect(),
action_id.clone(),
);

if let BlockStmtOrExpr::BlockStmt(block) = &mut *arrow.body {
block.visit_mut_with(&mut ClosureReplacer {
used_ids: &ids_from_closure,
Expand All @@ -503,7 +487,7 @@ impl<C: Comments> ServerActions<C> {
span: DUMMY_SP,
callee: quote_ident!("decryptActionBoundArgs").as_callee(),
args: vec![
action_id.as_arg(),
action_id.clone().as_arg(),
quote_ident!("$$ACTION_CLOSURE_BOUND").as_arg(),
],
..Default::default()
Expand Down Expand Up @@ -575,7 +559,29 @@ impl<C: Comments> ServerActions<C> {
.into(),
})));

Box::new(register_action_expr.clone())
self.hoisted_extra_items
.push(ModuleItem::Stmt(Stmt::Expr(ExprStmt {
span: DUMMY_SP,
expr: Box::new(annotate_ident_as_server_reference(
action_ident.clone(),
action_id.clone(),
arrow.span,
)),
})));

if ids_from_closure.is_empty() {
Box::new(action_ident.clone().into())
} else {
Box::new(bind_args_to_ident(
action_ident.clone(),
ids_from_closure
.iter()
.cloned()
.map(|id| Some(id.as_arg()))
.collect(),
action_id.clone(),
))
}
}

fn maybe_hoist_and_create_proxy_for_server_action_function(
Expand Down Expand Up @@ -609,20 +615,6 @@ impl<C: Comments> ServerActions<C> {
self.export_actions
.push((action_name.clone(), action_id.clone()));

let register_action_expr = bind_args_to_ref_expr(
annotate_ident_as_server_reference(
action_ident.clone(),
action_id.clone(),
function.span,
),
ids_from_closure
.iter()
.cloned()
.map(|id| Some(id.as_arg()))
.collect(),
action_id.clone(),
);

function.body.visit_mut_with(&mut ClosureReplacer {
used_ids: &ids_from_closure,
private_ctxt: self.private_ctxt,
Expand All @@ -646,7 +638,7 @@ impl<C: Comments> ServerActions<C> {
span: DUMMY_SP,
callee: quote_ident!("decryptActionBoundArgs").as_callee(),
args: vec![
action_id.as_arg(),
action_id.clone().as_arg(),
quote_ident!("$$ACTION_CLOSURE_BOUND").as_arg(),
],
..Default::default()
Expand Down Expand Up @@ -695,7 +687,29 @@ impl<C: Comments> ServerActions<C> {
.into(),
})));

Box::new(register_action_expr)
self.hoisted_extra_items
.push(ModuleItem::Stmt(Stmt::Expr(ExprStmt {
span: DUMMY_SP,
expr: Box::new(annotate_ident_as_server_reference(
action_ident.clone(),
action_id.clone(),
function.span,
)),
})));

if ids_from_closure.is_empty() {
Box::new(action_ident.clone().into())
} else {
Box::new(bind_args_to_ident(
action_ident.clone(),
ids_from_closure
.iter()
.cloned()
.map(|id| Some(id.as_arg()))
.collect(),
action_id.clone(),
))
}
}

fn maybe_hoist_and_create_proxy_for_cache_arrow_expr(
Expand Down Expand Up @@ -783,6 +797,16 @@ impl<C: Comments> ServerActions<C> {
.into(),
})));

self.hoisted_extra_items
.push(ModuleItem::Stmt(Stmt::Expr(ExprStmt {
span: DUMMY_SP,
expr: Box::new(annotate_ident_as_server_reference(
cache_ident.clone(),
reference_id.clone(),
arrow.span,
)),
})));

if let Some(Ident { sym, .. }) = &self.arrow_or_fn_expr_ident {
assign_name_to_ident(&cache_ident, sym.as_str(), &mut self.hoisted_extra_items);
}
Expand All @@ -793,41 +817,14 @@ impl<C: Comments> ServerActions<C> {
.map(|id| Some(id.as_arg()))
.collect();

let register_action_expr = annotate_ident_as_server_reference(
cache_ident.clone(),
reference_id.clone(),
arrow.span,
);

// If there're any bound args from the closure, we need to hoist the
// register action expression to the top-level, and return the bind
// expression inline.
if !bound_args.is_empty() {
let ref_ident = private_ident!(self.gen_ref_ident());

let ref_decl = VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(ref_ident.clone().into()),
init: Some(Box::new(register_action_expr.clone())),
definite: false,
}],
..Default::default()
};

// Hoist the register action expression to the top-level.
self.extra_items
.push(ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(ref_decl)))));

Box::new(bind_args_to_ref_expr(
Expr::Ident(ref_ident.clone()),
if bound_args.is_empty() {
Box::new(cache_ident.clone().into())
} else {
Box::new(bind_args_to_ident(
cache_ident.clone(),
bound_args,
reference_id.clone(),
))
} else {
Box::new(register_action_expr)
}
}

Expand Down Expand Up @@ -864,12 +861,6 @@ impl<C: Comments> ServerActions<C> {
self.export_actions
.push((cache_name.clone(), reference_id.clone()));

let register_action_expr = annotate_ident_as_server_reference(
cache_ident.clone(),
reference_id.clone(),
function.span,
);

function.body.visit_mut_with(&mut ClosureReplacer {
used_ids: &ids_from_closure,
private_ctxt: self.private_ctxt,
Expand Down Expand Up @@ -904,6 +895,16 @@ impl<C: Comments> ServerActions<C> {
.into(),
})));

self.hoisted_extra_items
.push(ModuleItem::Stmt(Stmt::Expr(ExprStmt {
span: DUMMY_SP,
expr: Box::new(annotate_ident_as_server_reference(
cache_ident.clone(),
reference_id.clone(),
function.span,
)),
})));

if let Some(Ident { sym, .. }) = fn_name {
assign_name_to_ident(&cache_ident, sym.as_str(), &mut self.hoisted_extra_items);
} else if self.in_default_export_decl {
Expand All @@ -916,35 +917,14 @@ impl<C: Comments> ServerActions<C> {
.map(|id| Some(id.as_arg()))
.collect();

// If there're any bound args from the closure, we need to hoist the
// register action expression to the top-level, and return the bind
// expression inline.
if !bound_args.is_empty() {
let ref_ident = private_ident!(self.gen_ref_ident());

let ref_decl = VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(ref_ident.clone().into()),
init: Some(Box::new(register_action_expr.clone())),
definite: false,
}],
..Default::default()
};

// Hoist the register action expression to the top-level.
self.extra_items
.push(ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(ref_decl)))));

Box::new(bind_args_to_ref_expr(
Expr::Ident(ref_ident.clone()),
if bound_args.is_empty() {
Box::new(cache_ident.clone().into())
} else {
Box::new(bind_args_to_ident(
cache_ident.clone(),
bound_args,
reference_id.clone(),
))
} else {
Box::new(register_action_expr)
}
}
}
Expand Down Expand Up @@ -2465,42 +2445,38 @@ fn annotate_ident_as_server_reference(ident: Ident, action_id: Atom, original_sp
})
}

fn bind_args_to_ref_expr(expr: Expr, bound: Vec<Option<ExprOrSpread>>, action_id: Atom) -> Expr {
if bound.is_empty() {
expr
} else {
// expr.bind(null, [encryptActionBoundArgs("id", arg1, arg2, ...)])
Expr::Call(CallExpr {
fn bind_args_to_ident(ident: Ident, bound: Vec<Option<ExprOrSpread>>, action_id: Atom) -> Expr {
// ident.bind(null, [encryptActionBoundArgs("id", arg1, arg2, ...)])
Expr::Call(CallExpr {
span: DUMMY_SP,
callee: Expr::Member(MemberExpr {
span: DUMMY_SP,
callee: Expr::Member(MemberExpr {
span: DUMMY_SP,
obj: Box::new(expr),
prop: MemberProp::Ident(quote_ident!("bind")),
})
.as_callee(),
args: vec![
ExprOrSpread {
spread: None,
expr: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
},
ExprOrSpread {
spread: None,
expr: Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: quote_ident!("encryptActionBoundArgs").as_callee(),
args: std::iter::once(ExprOrSpread {
spread: None,
expr: Box::new(action_id.into()),
})
.chain(bound.into_iter().flatten())
.collect(),
..Default::default()
})),
},
],
..Default::default()
obj: Box::new(ident.into()),
prop: MemberProp::Ident(quote_ident!("bind")),
})
}
.as_callee(),
args: vec![
ExprOrSpread {
spread: None,
expr: Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP }))),
},
ExprOrSpread {
spread: None,
expr: Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: quote_ident!("encryptActionBoundArgs").as_callee(),
args: std::iter::once(ExprOrSpread {
spread: None,
expr: Box::new(action_id.into()),
})
.chain(bound.into_iter().flatten())
.collect(),
..Default::default()
})),
},
],
..Default::default()
})
}

// Detects if two strings are similar (but not the same).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc
export const $$RSC_SERVER_ACTION_0 = async function foo() {
'use strict';
};
const foo = registerServerReference($$RSC_SERVER_ACTION_0, "006a88810ecce4a4e8b59d53b8327d7e98bbf251d7", null);
registerServerReference($$RSC_SERVER_ACTION_0, "006a88810ecce4a4e8b59d53b8327d7e98bbf251d7", null);
const foo = $$RSC_SERVER_ACTION_0;
const bar = async ()=>{
const x = 1;
// prettier-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import inter from '@next/font/google/target.css?{"path":"app/test.tsx","import":
export var $$RSC_SERVER_CACHE_0 = $$cache__("default", "c0dd5bb6fef67f5ab84327f5164ac2c3111a159337", 0, async function Cached({ children }) {
return <div className={inter.className}>{children}</div>;
});
registerServerReference($$RSC_SERVER_CACHE_0, "c0dd5bb6fef67f5ab84327f5164ac2c3111a159337", null);
Object["defineProperty"]($$RSC_SERVER_CACHE_0, "name", {
value: "Cached",
writable: false
});
export var Cached = registerServerReference($$RSC_SERVER_CACHE_0, "c0dd5bb6fef67f5ab84327f5164ac2c3111a159337", null);
export var Cached = $$RSC_SERVER_CACHE_0;
Copy link
Contributor Author

@unstubbable unstubbable Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One might wonder why we aren't using the original export name as the action/cache name, instead of generating the additional one. The reason is that the $$RSC_SERVER_ prefixed exports get a special treatment during tree shaking.

Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ export const $$RSC_SERVER_ACTION_0 = async function deleteItem($$ACTION_CLOSURE_
await deleteFromDb($$ACTION_ARG_0);
await deleteFromDb($$ACTION_ARG_1);
};
registerServerReference($$RSC_SERVER_ACTION_0, "406a88810ecce4a4e8b59d53b8327d7e98bbf251d7", null);
export function Item({ id1, id2 }) {
var deleteItem = registerServerReference($$RSC_SERVER_ACTION_0, "406a88810ecce4a4e8b59d53b8327d7e98bbf251d7", null).bind(null, encryptActionBoundArgs("406a88810ecce4a4e8b59d53b8327d7e98bbf251d7", id1, id2));
var deleteItem = $$RSC_SERVER_ACTION_0.bind(null, encryptActionBoundArgs("406a88810ecce4a4e8b59d53b8327d7e98bbf251d7", id1, id2));
return <Button action={deleteItem}>Delete</Button>;
}
export const $$RSC_SERVER_ACTION_1 = async function action($$ACTION_CLOSURE_BOUND) {
var [$$ACTION_ARG_0, $$ACTION_ARG_1] = await decryptActionBoundArgs("4090b5db271335765a4b0eab01f044b381b5ebd5cd", $$ACTION_CLOSURE_BOUND);
console.log($$ACTION_ARG_0);
console.log($$ACTION_ARG_1);
};
registerServerReference($$RSC_SERVER_ACTION_1, "4090b5db271335765a4b0eab01f044b381b5ebd5cd", null);
export default function Home() {
const info = {
name: 'John',
test: 'test'
};
const action = registerServerReference($$RSC_SERVER_ACTION_1, "4090b5db271335765a4b0eab01f044b381b5ebd5cd", null).bind(null, encryptActionBoundArgs("4090b5db271335765a4b0eab01f044b381b5ebd5cd", info.name, info.test));
const action = $$RSC_SERVER_ACTION_1.bind(null, encryptActionBoundArgs("4090b5db271335765a4b0eab01f044b381b5ebd5cd", info.name, info.test));
return null;
}
Loading
Loading