Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract generation of js_code into module #1169

Merged
merged 2 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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