Skip to content

Commit 6383f02

Browse files
committed
revert: client_value to push
1 parent 19acb51 commit 6383f02

File tree

3 files changed

+71
-62
lines changed

3 files changed

+71
-62
lines changed

packages/reactDom/src/ReactServerDOM.ml

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -90,31 +90,6 @@ module Fiber = struct
9090
mutable html_tag_attributes : Html.attribute_list;
9191
}
9292

93-
let model_to_chunk model index =
94-
Html.raw
95-
(Printf.sprintf "<script data-payload='%s'>window.srr_stream.push()</script>"
96-
(Html.single_quote_escape (Model.to_chunk model index)))
97-
98-
let boundary_to_chunk html index =
99-
let rc_replacement b s = Html.node "script" [] [ Html.raw (Printf.sprintf "$RC('B:%x', 'S:%x')" b s) ] in
100-
Html.list ~separator:"\n"
101-
[
102-
Html.node "div" [ Html.attribute "hidden" "true"; Html.attribute "id" (Printf.sprintf "S:%x" index) ] [ html ];
103-
rc_replacement index index;
104-
]
105-
106-
let html_suspense_immediate inner = Html.list [ Html.raw "<!--$-->"; inner; Html.raw "<!--/$-->" ]
107-
108-
let html_suspense_placeholder ~fallback id =
109-
Html.list
110-
[
111-
Html.raw "<!--$?-->";
112-
Html.node "template" [ Html.attribute "id" (Printf.sprintf "B:%x" id) ] [];
113-
fallback;
114-
Html.raw "<!--/$-->";
115-
]
116-
117-
let chunk_stream_end_script = Html.node "script" [] [ Html.raw "window.srr_stream.close()" ]
11893
let set_html_tag_attributes ~fiber html_attributes = fiber.html_tag_attributes <- html_attributes
11994
let push_hoisted_head ~fiber head = fiber.hoisted_head <- Some head
12095
let push_resource ~fiber resource = fiber.resources <- Resources.add resource fiber.resources
@@ -360,13 +335,8 @@ module Model = struct
360335
| Suspense { key; children; fallback } ->
361336
(* TODO: Need to check is_root? *)
362337
(* TODO: Maybe we need to push suspense index and suspense node separately *)
363-
(* Suspense boundaries should not be treated as root for their children *)
364-
let was_root = is_root.contents in
365-
is_root := false;
366338
let fallback = turn_element_into_payload fallback in
367-
let children_payload = turn_element_into_payload children in
368-
is_root := was_root;
369-
suspense_node ~key ~fallback [ children_payload ]
339+
suspense_node ~key ~fallback [ turn_element_into_payload children ]
370340
| Client_component { import_module; import_name; props; client = _ } ->
371341
let ref = component_ref ~module_:import_module ~name:import_name in
372342
let index = Stream.push ~context (to_chunk (Component_ref ref)) in
@@ -384,7 +354,11 @@ module Model = struct
384354
| Error error ->
385355
let index = Stream.push ~context (to_chunk (Error (env, error))) in
386356
`String (error_value index)
387-
| Element element -> element_to_payload ~context ?debug ~to_chunk ~env element
357+
| Element element ->
358+
let index =
359+
Stream.push ~context (to_chunk (Value (element_to_payload ~context ?debug ~to_chunk ~env element)))
360+
in
361+
`String (ref_value index)
388362
| Promise (promise, value_to_json) -> (
389363
match Lwt.state promise with
390364
| Return value ->
@@ -466,6 +440,32 @@ let rc_function_definition =
466440

467441
let rc_function_script = Html.node "script" [] [ Html.raw rc_function_definition ]
468442

443+
let model_to_chunk model index =
444+
Html.raw
445+
(Printf.sprintf "<script data-payload='%s'>window.srr_stream.push()</script>"
446+
(Html.single_quote_escape (Model.to_chunk model index)))
447+
448+
let boundary_to_chunk html index =
449+
let rc_replacement b s = Html.node "script" [] [ Html.raw (Printf.sprintf "$RC('B:%x', 'S:%x')" b s) ] in
450+
Html.list ~separator:"\n"
451+
[
452+
Html.node "div" [ Html.attribute "hidden" "true"; Html.attribute "id" (Printf.sprintf "S:%x" index) ] [ html ];
453+
rc_replacement index index;
454+
]
455+
456+
let html_suspense_immediate inner = Html.list [ Html.raw "<!--$-->"; inner; Html.raw "<!--/$-->" ]
457+
458+
let html_suspense_placeholder ~fallback id =
459+
Html.list
460+
[
461+
Html.raw "<!--$?-->";
462+
Html.node "template" [ Html.attribute "id" (Printf.sprintf "B:%x" id) ] [];
463+
fallback;
464+
Html.raw "<!--/$-->";
465+
]
466+
467+
let chunk_stream_end_script = Html.node "script" [] [ Html.raw "window.srr_stream.close()" ]
468+
469469
let rec client_to_html ~(fiber : Fiber.t) (element : React.element) =
470470
match element with
471471
| Empty -> Lwt.return Html.null
@@ -486,7 +486,7 @@ let rec client_to_html ~(fiber : Fiber.t) (element : React.element) =
486486
match prop with
487487
| React.JSX.Action (_, key, f) ->
488488
let json = `Assoc [ ("id", `String f.id); ("bound", `Null) ] in
489-
let index = Stream.push ~context (Fiber.model_to_chunk (Value json)) in
489+
let index = Stream.push ~context (model_to_chunk (Value json)) in
490490
React.JSX.String (key, key, Model.action_value index)
491491
| _ -> prop)
492492
attributes
@@ -513,10 +513,10 @@ let rec client_to_html ~(fiber : Fiber.t) (element : React.element) =
513513
let context = fiber.context in
514514
let async =
515515
let%lwt html = children |> client_to_html ~fiber in
516-
Lwt.return (Fiber.boundary_to_chunk html)
516+
Lwt.return (boundary_to_chunk html)
517517
in
518518
let index = Stream.push_async ~context async in
519-
let sync = Fiber.html_suspense_placeholder ~fallback index in
519+
let sync = html_suspense_placeholder ~fallback index in
520520
Lwt.return sync
521521
| Client_component { import_module = _; import_name = _; props = _; client } -> client_to_html ~fiber client
522522
| Provider children -> client_to_html ~fiber children
@@ -566,10 +566,10 @@ let rec render_element_to_html ~(fiber : Fiber.t) (element : React.element) : (H
566566
| Client_component { import_module; import_name; props; client } ->
567567
let context = fiber.context in
568568
let env = fiber.env in
569-
let props = Model.client_values_to_json ~context ~to_chunk:Fiber.model_to_chunk ~env props in
569+
let props = Model.client_values_to_json ~context ~to_chunk:model_to_chunk ~env props in
570570
let%lwt html = client_to_html ~fiber client in
571571
let ref : json = Model.component_ref ~module_:import_module ~name:import_name in
572-
let index = Stream.push ~context (Fiber.model_to_chunk (Component_ref ref)) in
572+
let index = Stream.push ~context (model_to_chunk (Component_ref ref)) in
573573
let model = Model.node ~tag:(Model.ref_value index) ~props [] in
574574
Lwt.return (html, model)
575575
| Suspense { key; children; fallback } -> (
@@ -582,33 +582,31 @@ let rec render_element_to_html ~(fiber : Fiber.t) (element : React.element) : (H
582582
let promise =
583583
try%lwt
584584
let%lwt html, model = promise in
585-
let to_chunk index =
586-
Html.list [ Fiber.boundary_to_chunk html index; Fiber.model_to_chunk (Value model) index ]
587-
in
585+
let to_chunk index = Html.list [ boundary_to_chunk html index; model_to_chunk (Value model) index ] in
588586
Lwt.return to_chunk
589587
with exn ->
590588
let message = Printexc.to_string exn in
591589
let stack = create_stack_trace () in
592590
let error = make_error ~message ~stack ~digest:"" in
593-
let to_chunk index = Fiber.model_to_chunk (Error (fiber.env, error)) index in
591+
let to_chunk index = model_to_chunk (Error (fiber.env, error)) index in
594592
Lwt.return to_chunk
595593
in
596594
let index = Stream.push_async ~context promise in
597595
Lwt.return
598-
( Fiber.html_suspense_placeholder ~fallback:html_fallback index,
596+
( html_suspense_placeholder ~fallback:html_fallback index,
599597
Model.suspense_placeholder ~key ~fallback:model_fallback index )
600598
| Return (html, model) ->
601599
let model = Model.suspense_node ~key ~fallback:model_fallback [ model ] in
602-
Lwt.return (Fiber.html_suspense_immediate html, model)
600+
Lwt.return (html_suspense_immediate html, model)
603601
| Fail exn -> Lwt.reraise exn
604602
with exn ->
605603
let context = fiber.context in
606604
let error = Model.exn_to_error exn in
607605
let to_chunk index =
608-
Html.list [ Fiber.model_to_chunk (Error (fiber.env, error)) index; Fiber.boundary_to_chunk Html.null index ]
606+
Html.list [ model_to_chunk (Error (fiber.env, error)) index; boundary_to_chunk Html.null index ]
609607
in
610608
let index = Stream.push ~context to_chunk in
611-
let html = Fiber.html_suspense_placeholder ~fallback:html_fallback index in
609+
let html = html_suspense_placeholder ~fallback:html_fallback index in
612610
Lwt.return (html, Model.suspense_placeholder ~key ~fallback:model_fallback index))
613611
| Provider children -> render_element_to_html ~fiber children
614612
| Consumer children -> render_element_to_html ~fiber children
@@ -689,7 +687,7 @@ and render_regular_element ~fiber ~key ~tag ~attributes ~children ~inner_html ()
689687
(fun prop ->
690688
match prop with
691689
| React.JSX.Action (_, key, f) ->
692-
let html = Fiber.model_to_chunk (Value (`Assoc [ ("id", `String f.id); ("bound", `Null) ])) in
690+
let html = model_to_chunk (Value (`Assoc [ ("id", `String f.id); ("bound", `Null) ])) in
693691
let index = Stream.push ~context html in
694692
React.JSX.String (key, key, Model.action_value index)
695693
| _ -> prop)
@@ -791,7 +789,7 @@ let render_html ?(skipRoot = false) ?(env = `Dev) ?debug:(_ = false) ?bootstrapS
791789
in
792790
let%lwt root_html, root_model = render_element_to_html ~fiber element in
793791
(* To return the model value immediately, we don't push it to the stream but return it as a payload script together with the user_scripts *)
794-
let root_data_payload = Fiber.model_to_chunk (Value root_model) 0 in
792+
let root_data_payload = model_to_chunk (Value root_model) 0 in
795793
(* In case of not having any task pending, we can close the stream *)
796794
(match context.pending = 0 with true -> context.close () | false -> ());
797795
let bootstrap_script_content =
@@ -855,7 +853,7 @@ let render_html ?(skipRoot = false) ?(env = `Dev) ?debug:(_ = false) ?bootstrapS
855853
let subscribe fn =
856854
let fn_with_to_string v = fn (Html.to_string v) in
857855
let%lwt () = Push_stream.subscribe ~fn:fn_with_to_string stream in
858-
fn_with_to_string Fiber.chunk_stream_end_script
856+
fn_with_to_string chunk_stream_end_script
859857
in
860858
Lwt.return (Html.to_string html, subscribe)
861859

packages/reactDom/test/test_RSC_html.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,13 @@ let client_with_element_props () =
339339
in
340340
assert_html (app ())
341341
~shell:
342-
"Client with elment prop<script \
343-
data-payload='0:[\"$\",\"$1\",null,{\"element\":[\"$\",\"span\",null,{\"children\":\"server-component-as-props-to-client-component\"},null,[],{}]},null,[],{}]\n\
342+
"Client with elment prop<script data-payload='0:[\"$\",\"$2\",null,{\"element\":\"$1\"},null,[],{}]\n\
344343
'>window.srr_stream.push()</script>"
345344
[
346-
"<script data-payload='1:I[\"./client-with-props.js\",[],\"ClientWithProps\"]\n\
345+
"<script \
346+
data-payload='1:[\"$\",\"span\",null,{\"children\":\"server-component-as-props-to-client-component\"},null,[],{}]\n\
347+
'>window.srr_stream.push()</script>";
348+
"<script data-payload='2:I[\"./client-with-props.js\",[],\"ClientWithProps\"]\n\
347349
'>window.srr_stream.push()</script>";
348350
]
349351

packages/reactDom/test/test_RSC_model.ml

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,9 @@ let client_with_element_props () =
529529
assert_list_of_strings !output
530530
[
531531
"1:I[\"./client-with-props.js\",[],\"ClientWithProps\"]\n";
532-
"0:[[\"$\",\"div\",null,{\"children\":\"Server Content\"},null,[],{}],[\"$\",\"$1\",null,{\"children\":\"Client \
533-
Content\"},null,[],{}]]\n";
532+
"2:\"Client Content\"\n";
533+
"0:[[\"$\",\"div\",null,{\"children\":\"Server \
534+
Content\"},null,[],{}],[\"$\",\"$1\",null,{\"children\":\"$2\"},null,[],{}]]\n";
534535
];
535536
Lwt.return ()
536537

@@ -657,9 +658,9 @@ let client_with_server_children () =
657658
assert_list_of_strings !output
658659
[
659660
"1:I[\"./client-with-server-children.js\",[],\"ClientWithServerChildren\"]\n";
661+
"2:[\"$\",\"div\",null,{\"children\":\"Server Component Inside Client\"},null,[],{}]\n";
660662
"0:[[\"$\",\"div\",null,{\"children\":\"Server \
661-
Content\"},null,[],{}],[\"$\",\"$1\",null,{\"children\":[\"$\",\"div\",null,{\"children\":\"Server Component \
662-
Inside Client\"},null,[],{}]},null,[],{}]]\n";
663+
Content\"},null,[],{}],[\"$\",\"$1\",null,{\"children\":\"$2\"},null,[],{}]]\n";
663664
];
664665
Lwt.return ()
665666

@@ -904,13 +905,21 @@ let nested_context () =
904905
"4:I[\"./provider.js\",[],\"Provider\"]\n";
905906
"6:I[\"./provider.js\",[],\"Provider\"]\n";
906907
"8:I[\"./provider.js\",[],\"Provider\"]\n";
907-
"7:[\"$\",\"$8\",null,{\"value\":null,\"children\":\"Hey you\"},null,[],{}]\n";
908-
"9:I[\"./consumer.js\",[],\"Consumer\"]\n";
909-
"5:[\"$\",\"$6\",null,{\"value\":\"$7\",\"children\":[\"/me\",[\"$\",\"$9\",null,{},null,[],{}]]},null,[],{}]\n";
910-
"a:I[\"./consumer.js\",[],\"Consumer\"]\n";
911-
"3:[\"$\",\"$4\",null,{\"value\":\"$5\",\"children\":[\"/about\",[\"$\",\"$a\",null,{},null,[],{}]]},null,[],{}]\n";
912-
"b:I[\"./consumer.js\",[],\"Consumer\"]\n";
913-
"1:[\"$\",\"$2\",null,{\"value\":\"$3\",\"children\":[\"/root\",[\"$\",\"$b\",null,{},null,[],{}]]},null,[],{}]\n";
908+
"9:null\n";
909+
"a:\"Hey you\"\n";
910+
"7:[\"$\",\"$8\",null,{\"value\":\"$9\",\"children\":\"$a\"},null,[],{}]\n";
911+
"b:\"$7\"\n";
912+
"c:I[\"./consumer.js\",[],\"Consumer\"]\n";
913+
"d:[\"/me\",[\"$\",\"$c\",null,{},null,[],{}]]\n";
914+
"5:[\"$\",\"$6\",null,{\"value\":\"$b\",\"children\":\"$d\"},null,[],{}]\n";
915+
"e:\"$5\"\n";
916+
"f:I[\"./consumer.js\",[],\"Consumer\"]\n";
917+
"10:[\"/about\",[\"$\",\"$f\",null,{},null,[],{}]]\n";
918+
"3:[\"$\",\"$4\",null,{\"value\":\"$e\",\"children\":\"$10\"},null,[],{}]\n";
919+
"11:\"$3\"\n";
920+
"12:I[\"./consumer.js\",[],\"Consumer\"]\n";
921+
"13:[\"/root\",[\"$\",\"$12\",null,{},null,[],{}]]\n";
922+
"1:[\"$\",\"$2\",null,{\"value\":\"$11\",\"children\":\"$13\"},null,[],{}]\n";
914923
"0:\"$1\"\n";
915924
];
916925
Lwt.return ()

0 commit comments

Comments
 (0)