@@ -101,6 +101,16 @@ def stream_react_component(component_name, options = {})
101101 )
102102 end
103103
104+ def stream_react_component_async ( component_name , options = { } )
105+ Fiber . new do
106+ stream = stream_react_component ( component_name , options )
107+ stream . each_chunk do |chunk |
108+ Fiber . yield chunk
109+ end
110+ Fiber . yield nil
111+ end
112+ end
113+
104114 # react_component_hash is used to return multiple HTML strings for server rendering, such as for
105115 # adding meta-tags to a page.
106116 # It is exactly like react_component except for the following:
@@ -376,25 +386,24 @@ def build_react_component_result_for_server_streamed_content(
376386 component_specification_tag : required ( "component_specification_tag" ) ,
377387 render_options : required ( "render_options" )
378388 )
389+ content_tag_options_html_tag = render_options . html_options [ :tag ] || "div"
379390 # The component_specification_tag is appended to the first chunk
380391 # We need to pass it early with the first chunk because it's needed in hydration
381392 # We need to make sure that client can hydrate the app early even before all components are streamed
382393 is_first_chunk = true
383394 rendered_html_stream = rendered_html_stream . transform do |chunk |
384395 if is_first_chunk
385396 is_first_chunk = false
386- next "#{ chunk } \n #{ component_specification_tag } "
397+ next <<-HTML
398+ #{ rails_context_if_not_already_rendered }
399+ #{ component_specification_tag }
400+ < #{ content_tag_options_html_tag } id="#{ render_options . dom_id } "> #{ chunk } </ #{ content_tag_options_html_tag } >
401+ HTML
387402 end
388403 chunk
389404 end
390405
391- content_tag_options_html_tag = render_options . html_options [ :tag ] || "div"
392- rendered_html_stream = rendered_html_stream . prepend { rails_context_if_not_already_rendered }
393- . prepend { "<#{ content_tag_options_html_tag } id=\" #{ render_options . dom_id } \" >" }
394- . transform ( &:html_safe )
395-
396- rendered_html_stream . append { "</#{ content_tag_options_html_tag } >" }
397- . append { component_specification_tag }
406+ rendered_html_stream . transform ( &:html_safe )
398407 # TODO: handle console logs
399408 end
400409
0 commit comments