@@ -348,15 +348,14 @@ module Model = struct
348348 `String (promise_value index)
349349 | Sleep ->
350350 let promise =
351- Lwt. map to_chunk
352- (try % lwt
353- let % lwt value = promise in
354- Lwt. return (Value (value_to_json value))
355- with exn ->
356- let message = Printexc. to_string exn in
357- let stack = create_stack_trace () in
358- let error = make_error ~message ~stack ~digest: " " in
359- Lwt. return (Error (env, error)))
351+ try % lwt
352+ let % lwt value = promise in
353+ Lwt. return (to_chunk (Value (value_to_json value)))
354+ with exn ->
355+ let message = Printexc. to_string exn in
356+ let stack = create_stack_trace () in
357+ let error = make_error ~message ~stack ~digest: " " in
358+ Lwt. return (to_chunk (Error (env, error)))
360359 in
361360 let index = Stream. push_async promise ~context in
362361 `String (promise_value index)
@@ -788,7 +787,7 @@ let render_html ?(skipRoot = false) ?(env = `Dev) ?bootstrapScriptContent ?boots
788787 modules
789788 | None -> [] )
790789 in
791- (* Initial index is 1 because 0 is reserved for the root chunk *)
790+ (* Since we don't push the root_data_payload to the stream but return it immediately with the initial HTML, the stream's initial index starts at 1, with index 0 reserved for the root_data_payload *)
792791 let stream, context = Stream. make ~initial_index: 1 in
793792 let fiber : Fiber.t =
794793 {
@@ -804,8 +803,8 @@ let render_html ?(skipRoot = false) ?(env = `Dev) ?bootstrapScriptContent ?boots
804803 }
805804 in
806805 let % lwt root_html, root_model = Fiber. render_element_to_html ~fiber element in
807- (* We don't push the root chunk to the stream, it's added to user_scripts *)
808- let root_chunk = Fiber. model_to_chunk (Value root_model) 0 in
806+ (* 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 *)
807+ let root_data_payload = Fiber. model_to_chunk (Value root_model) 0 in
809808 (* In case of not having any task pending, we can close the stream *)
810809 (match context.pending = 0 with true -> context.close () | false -> () );
811810 let bootstrap_script_content =
@@ -836,7 +835,7 @@ let render_html ?(skipRoot = false) ?(env = `Dev) ?bootstrapScriptContent ?boots
836835 [
837836 rc_function_script;
838837 rsc_start_script;
839- root_chunk ;
838+ root_data_payload ;
840839 bootstrap_script_content;
841840 bootstrap_scripts_nodes;
842841 bootstrap_modules_nodes;
0 commit comments