Skip to content

Commit

Permalink
Extract generation of js_code into module (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashgaliyev authored and justin808 committed Dec 6, 2018
1 parent 735c434 commit aa7060f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/react_on_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require "react_on_rails/version_checker"
require "react_on_rails/configuration"
require "react_on_rails/server_rendering_pool"
require "react_on_rails/server_rendering_js_code"
require "react_on_rails/engine"
require "react_on_rails/react_component/render_options"
require "react_on_rails/version_syntax_converter"
Expand Down
23 changes: 7 additions & 16 deletions lib/react_on_rails/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,22 +431,13 @@ def server_rendered_react_component(render_options)
#
# Read more here: http://timelessrepo.com/json-isnt-a-javascript-subset

# rubocop:disable Layout/IndentHeredoc
js_code = <<-JS
(function() {
var railsContext = #{rails_context(server_side: true).to_json};
#{initialize_redux_stores}
var props = #{props_string(props).gsub("\u2028", '\u2028').gsub("\u2029", '\u2029')};
return ReactOnRails.serverRenderReactComponent({
name: '#{react_component_name}',
domNodeId: '#{render_options.dom_id}',
props: props,
trace: #{render_options.trace},
railsContext: railsContext
});
})()
JS
# rubocop:enable Layout/IndentHeredoc
js_code = ReactOnRails::ServerRenderingJsCode.server_rendering_component_js_code(
props_string: props_string(props).gsub("\u2028", '\u2028').gsub("\u2029", '\u2029'),
rails_context: rails_context(server_side: true).to_json,
redux_stores: initialize_redux_stores,
react_component_name: react_component_name,
render_options: render_options
)

begin
result = ReactOnRails::ServerRenderingPool.server_render_js_with_console_logging(js_code, render_options)
Expand Down
42 changes: 42 additions & 0 deletions lib/react_on_rails/server_rendering_js_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module ReactOnRails
module ServerRenderingJsCode
class << self
def js_code_renderer
@js_code_renderer ||= if ReactOnRails::Utils.react_on_rails_pro?
ReactOnRailsPro::ServerRenderingJsCode
else
self
end
end

def server_rendering_component_js_code(
props_string: nil,
rails_context: nil,
redux_stores: nil,
react_component_name: nil,
render_options: nil
)
js_code_renderer.render(props_string, rails_context, redux_stores, react_component_name, render_options)
end

def render(props_string, rails_context, redux_stores, react_component_name, render_options)
<<-JS
(function() {
var railsContext = #{rails_context};
#{redux_stores}
var props = #{props_string};
return ReactOnRails.serverRenderReactComponent({
name: '#{react_component_name}',
domNodeId: '#{render_options.dom_id}',
props: props,
trace: #{render_options.trace},
railsContext: railsContext
});
})()
JS
end
end
end
end

0 comments on commit aa7060f

Please sign in to comment.