From aa7060fd93e5b6436b4de57b55885dc095642aab Mon Sep 17 00:00:00 2001 From: Andrey Ashgaliyev Date: Thu, 6 Dec 2018 06:27:37 +0200 Subject: [PATCH] Extract generation of js_code into module (#1169) --- lib/react_on_rails.rb | 1 + lib/react_on_rails/helper.rb | 23 ++++------ .../server_rendering_js_code.rb | 42 +++++++++++++++++++ 3 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 lib/react_on_rails/server_rendering_js_code.rb diff --git a/lib/react_on_rails.rb b/lib/react_on_rails.rb index 7a30bae22..81a07f600 100644 --- a/lib/react_on_rails.rb +++ b/lib/react_on_rails.rb @@ -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" diff --git a/lib/react_on_rails/helper.rb b/lib/react_on_rails/helper.rb index ca5dd9b09..0b56745f9 100644 --- a/lib/react_on_rails/helper.rb +++ b/lib/react_on_rails/helper.rb @@ -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) diff --git a/lib/react_on_rails/server_rendering_js_code.rb b/lib/react_on_rails/server_rendering_js_code.rb new file mode 100644 index 000000000..04f58490c --- /dev/null +++ b/lib/react_on_rails/server_rendering_js_code.rb @@ -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