Skip to content

Commit 8f971d7

Browse files
JSX v4: keep an async component's @directive on its own function
The JSX PPX moved every attribute of a component's function onto the generated props => make(props) wrapper. For an async component that wrapper is a sync function returning make's promise, so @react.component let make = @directive("'use cache'") async (~id) => ... compiled to function Name(props) { 'use cache'; return make(props); }, which Next.js rejects ("'use cache' functions must be async"). A directive on an async component now stays on make, which the wrapper then reduces to. Sync components are unchanged: their wrapper inlines make, so the directive already lands on the one function. Co-authored-by: Jaap Frolich <jfrolich@gmail.com> Session: https://os.tella.dev/session/os-01a1c0bb-9d82-7375-9b3e-9714de65ddf9
1 parent b82d944 commit 8f971d7

6 files changed

Lines changed: 169 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#### :bug: Bug fix
1919

20+
- JSX v4: keep `@directive` on an async component's own function instead of its generated sync wrapper, so `@react.component let make = @directive("'use cache'") async (...) => ...` compiles to an async function starting with `'use cache'`, which Next.js accepts.
21+
2022
#### :memo: Documentation
2123

2224
#### :nail_care: Polish

‎compiler/syntax/src/jsx_v4.ml‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ let filename_from_loc (pstr_loc : Location.t) =
7979
file_name
8080

8181
(* Build a string representation of a module name with segments separated by $ *)
82+
(* An async component's code runs in the inner [make]; the generated wrapper
83+
only returns its promise. A directive ('use cache', 'use server') has to sit
84+
on the async function, so it stays on [make]. A sync wrapper has [make]
85+
inlined into it, so there the directive stays on the wrapper. Returns the
86+
wrapper's attributes and [make]'s. *)
87+
let split_directive_attrs ~async attrs =
88+
if async then List.partition (fun ({txt}, _) -> txt <> "directive") attrs
89+
else (attrs, [])
90+
8291
let make_module_name file_name nested_modules fn_name =
8392
let full_module_name =
8493
match (file_name, nested_modules, fn_name) with
@@ -602,6 +611,9 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name binding =
602611
modified_binding ~binding_loc ~binding_pat_loc ~fn_name binding
603612
in
604613
let is_async = Ast_async.dig_async_payload_from_function binding.pvb_expr in
614+
let wrapper_attrs, make_attrs =
615+
split_directive_attrs ~async:is_async binding.pvb_expr.pexp_attributes
616+
in
605617
let named_arg_list, newtypes, _typeConstraints =
606618
recursively_transform_named_args_for_make
607619
(modified_binding_old binding)
@@ -652,7 +664,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name binding =
652664
]
653665
else [props_param]
654666
in
655-
Exp.fun_ ~attrs:binding.pvb_expr.pexp_attributes params inner_expression
667+
Exp.fun_ ~attrs:wrapper_attrs params inner_expression
656668
in
657669
let full_expression =
658670
match full_module_name with
@@ -801,7 +813,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name binding =
801813
| _ -> [Typ.any ()]))))
802814
in
803815
(* Add the collected newtypes (type a b c) to make's definition *)
804-
Exp.fun_ ~async:is_async ~newtypes:(List.rev newtypes)
816+
Exp.fun_ ~attrs:make_attrs ~async:is_async ~newtypes:(List.rev newtypes)
805817
(props_param :: nolabel_params)
806818
expression
807819
in
@@ -825,6 +837,9 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name binding =
825837
let is_async =
826838
Ast_async.dig_async_payload_from_function modified_binding.pvb_expr
827839
in
840+
let wrapper_attrs, make_attrs =
841+
split_directive_attrs ~async:is_async binding.pvb_expr.pexp_attributes
842+
in
828843

829844
let make_new_binding ~loc ~full_module_name binding =
830845
let props_pattern =
@@ -884,7 +899,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name binding =
884899
in
885900
let applied_expression = constrain_jsx_return applied_expression in
886901
let wrapper_expr =
887-
Exp.fun_ ~attrs:binding.pvb_expr.pexp_attributes
902+
Exp.fun_ ~attrs:wrapper_attrs
888903
[Exp.fun_param Nolabel props_pattern]
889904
applied_expression
890905
in
@@ -908,8 +923,8 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name binding =
908923
let binding_expr =
909924
{
910925
binding.pvb_expr with
911-
(* moved to wrapper_expr *)
912-
pexp_attributes = [];
926+
(* moved to wrapper_expr, but for a directive on an async component *)
927+
pexp_attributes = make_attrs;
913928
}
914929
in
915930
( None,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Async = {
2+
@react.component
3+
let make =
4+
@directive("'use cache';") async (~id: string) => <div>{React.string(id)}</div>
5+
}
6+
7+
module AsyncWithProps = {
8+
type props = {id: string}
9+
10+
@react.componentWithProps
11+
let make =
12+
@directive("'use cache';") async (props: props) => <div>{React.string(props.id)}</div>
13+
}
14+
15+
module Sync = {
16+
@react.component
17+
let make =
18+
@directive("'use no memo';") (~id: string) => <div>{React.string(id)}</div>
19+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module Async = {
2+
@res.jsxComponentProps
3+
type props<'id> = {
4+
id: 'id,
5+
}
6+
7+
let make =
8+
@directive("'use cache';") async ({id, _}: props<string>): React.element =>
9+
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.string(id)})})
10+
11+
let make = React.component({
12+
let \"AsyncDirective$Async" = (props: props<_>) => Jsx.promise(make(props))
13+
\"AsyncDirective$Async"
14+
})
15+
}
16+
17+
module AsyncWithProps = {
18+
type props = {id: string}
19+
20+
let make =
21+
@directive("'use cache';") async (props: props): React.element =>
22+
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.string(props.id)})})
23+
24+
let make = React.component({
25+
let \"AsyncDirective$AsyncWithProps" = (props: props): React.element => Jsx.promise(make(props))
26+
\"AsyncDirective$AsyncWithProps"
27+
})
28+
}
29+
30+
module Sync = {
31+
@res.jsxComponentProps
32+
type props<'id> = {
33+
id: 'id,
34+
}
35+
36+
let make = ({id, _}: props<string>): React.element =>
37+
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.string(id)})})
38+
39+
let make = React.component({
40+
let \"AsyncDirective$Sync" = @directive("'use no memo';") (props: props<_>) => make(props)
41+
42+
\"AsyncDirective$Sync"
43+
})
44+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as JsxRuntime from "react/jsx-runtime";
4+
5+
async function make(param) {
6+
'use cache';
7+
return JsxRuntime.jsx("div", {
8+
children: param.id
9+
});
10+
}
11+
12+
let Async_jsx_directive$Cached = make;
13+
14+
let Cached = {
15+
make: Async_jsx_directive$Cached
16+
};
17+
18+
async function make$1(props) {
19+
'use cache';
20+
return JsxRuntime.jsx("div", {
21+
children: props.id
22+
});
23+
}
24+
25+
let Async_jsx_directive$CachedWithProps = make$1;
26+
27+
let CachedWithProps = {
28+
make: Async_jsx_directive$CachedWithProps
29+
};
30+
31+
function Async_jsx_directive$NoMemo(props) {
32+
'use no memo';
33+
return JsxRuntime.jsx("div", {
34+
children: props.id
35+
});
36+
}
37+
38+
let NoMemo = {
39+
make: Async_jsx_directive$NoMemo
40+
};
41+
42+
let Cached$make = Async_jsx_directive$Cached;
43+
44+
let CachedWithProps$make = Async_jsx_directive$CachedWithProps;
45+
46+
let NoMemo$make = Async_jsx_directive$NoMemo;
47+
48+
export {
49+
Cached,
50+
CachedWithProps,
51+
NoMemo,
52+
Cached$make,
53+
CachedWithProps$make,
54+
NoMemo$make,
55+
}
56+
/* react/jsx-runtime Not a pure module */
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@@config({
2+
flags: ["-bs-jsx", "4"],
3+
})
4+
5+
// A directive on an async component belongs on the async function itself,
6+
// not on the generated sync wrapper: 'use cache' on a sync function is
7+
// rejected by Next.js.
8+
9+
module Cached = {
10+
@react.component
11+
let make =
12+
@directive("'use cache'") async (~id: string) => <div>{React.string(id)}</div>
13+
}
14+
15+
module CachedWithProps = {
16+
type props = {id: string}
17+
18+
@react.componentWithProps
19+
let make =
20+
@directive("'use cache'") async (props: props) => <div>{React.string(props.id)}</div>
21+
}
22+
23+
// A sync component's wrapper inlines its body, so it keeps the directive.
24+
module NoMemo = {
25+
@react.component
26+
let make =
27+
@directive("'use no memo'") (~id: string) => <div>{React.string(id)}</div>
28+
}

0 commit comments

Comments
 (0)