@@ -101,6 +101,16 @@ def stream_react_component(component_name, options = {})
101
101
)
102
102
end
103
103
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
+
104
114
# react_component_hash is used to return multiple HTML strings for server rendering, such as for
105
115
# adding meta-tags to a page.
106
116
# It is exactly like react_component except for the following:
@@ -376,25 +386,24 @@ def build_react_component_result_for_server_streamed_content(
376
386
component_specification_tag : required ( "component_specification_tag" ) ,
377
387
render_options : required ( "render_options" )
378
388
)
389
+ content_tag_options_html_tag = render_options . html_options [ :tag ] || "div"
379
390
# The component_specification_tag is appended to the first chunk
380
391
# We need to pass it early with the first chunk because it's needed in hydration
381
392
# We need to make sure that client can hydrate the app early even before all components are streamed
382
393
is_first_chunk = true
383
394
rendered_html_stream = rendered_html_stream . transform do |chunk |
384
395
if is_first_chunk
385
396
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
387
402
end
388
403
chunk
389
404
end
390
405
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 )
398
407
# TODO: handle console logs
399
408
end
400
409
0 commit comments