react_on_rails 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47bf68c00a2b017047097d27b50e92d6f417b071
4
- data.tar.gz: 9eca8eecbee44ad2c857680735b44e61027f365c
3
+ metadata.gz: 4a68c1b72267dce9d0864c0e21429df7e89011e8
4
+ data.tar.gz: 1bbf750bdf7b128fba4c393a7716b2eef962674e
5
5
  SHA512:
6
- metadata.gz: 566649c85b68a57e26d9ac79bcd9f2b23802c288eb15c4032542fdbe96bf79b6fd9121ced75bb73fe28e1d20ba235e0b155e10d00ac5ae63a64d8db6aa2e4781
7
- data.tar.gz: 4ca24d4f09242dd39d0c7842a3c9383c47f9515720bb78a5dd6e575ec0657d293ab2f343000db7d6a4df4306a159374e77d9e67a7afd67650bae3d7d14b7a3b4
6
+ metadata.gz: 82077ea1833158fb45c76c6ab5de0235e42f753257ff7f9a254938db05b2fe9a4262a126f729224a1be1f838139866111e0cefd6a4068f075c96b32dbb3ee616
7
+ data.tar.gz: b0f8347d48219798ceffede4bd9d83c5eeb9b8f0509913fac16a6ab22a5b5dc2eb6d9bff094ce1ce7a23f2bcbf6555245dcbc43c388cb335dc91edc3c8536321
data/README.md CHANGED
@@ -102,6 +102,7 @@ Params are:
102
102
  * **prerender**: <true/false> set to false when debugging!
103
103
  * **trace**: <true/false> set to true to print additional debugging information in the browser default is true for development, off otherwise
104
104
  * **replay_console**: <true/false> Default is true. False will disable echoing server rendering logs, which can make troubleshooting server rendering difficult.
105
+ * Any other options are passed to the content tag, including the id.
105
106
 
106
107
  ## JavaScript
107
108
 
@@ -157,6 +158,7 @@ ReactOnRails.configure do |config|
157
158
 
158
159
  # For server rendering. This can be set to false so that server side messages are discarded.
159
160
  config.replay_console = true # Default is true. Be cautious about turning this off.
161
+ config.logging_on_server = true # Default is true. Logs server rendering messags to Rails.logger.info
160
162
  end
161
163
  ```
162
164
 
@@ -284,6 +286,10 @@ The gem is available as open source under the terms of the [MIT License](http://
284
286
 
285
287
  See https://github.com/svenfuchs/gem-release
286
288
 
289
+ ```bash
290
+ gem bump --tag --release
291
+ ```
292
+
287
293
  # Authors
288
294
  The Shaka Code team!
289
295
 
@@ -30,7 +30,10 @@ module ReactOnRailsHelper
30
30
  # trace: <true/false> set to true to print additional debugging information in the browser
31
31
  # default is true for development, off otherwise
32
32
  # replay_console: <true/false> Default is true. False will disable echoing server rendering
33
- # logs, which can make troubleshooting server rendering difficult.
33
+ # logs to browser. While this can make troubleshooting server rendering difficult,
34
+ # so long as you have the default configuration of logging_on_server set to
35
+ # true, you'll still see the errors on the server.
36
+ # Any other options are passed to the content tag, including the id.
34
37
  def react_component(component_name, props = {}, options = {})
35
38
  # Create the JavaScript and HTML to allow either client or server rendering of the
36
39
  # react_component.
@@ -41,16 +44,21 @@ module ReactOnRailsHelper
41
44
  # We use this react_component_index in case we have the same component multiple times on the page.
42
45
  react_component_index = next_react_component_index
43
46
  react_component_name = component_name.camelize # Not sure if we should be doing this (JG)
44
- dom_id = "#{component_name}-react-component-#{react_component_index}"
47
+ if options[:id].nil?
48
+ dom_id = "#{component_name}-react-component-#{react_component_index}"
49
+ else
50
+ dom_id = options[:id]
51
+ end
45
52
 
46
53
  # Setup the page_loaded_js, which is the same regardless of prerendering or not!
47
54
  # The reason is that React is smart about not doing extra work if the server rendering did its job.
48
55
  data_variable_name = "__#{component_name.camelize(:lower)}Data#{react_component_index}__"
49
56
  turbolinks_loaded = Object.const_defined?(:Turbolinks)
50
57
  install_render_events = turbolinks_loaded ? turbolinks_bootstrap(dom_id) : non_turbolinks_bootstrap
58
+ props_string = props.is_a?(String) ? props : props.to_json
51
59
  page_loaded_js = <<-JS
52
60
  (function() {
53
- window.#{data_variable_name} = #{props.to_json};
61
+ window.#{data_variable_name} = #{props_string};
54
62
  #{define_render_if_dom_node_present(react_component_name, data_variable_name, dom_id,
55
63
  trace(options), generator_function(options))}
56
64
  #{install_render_events}
@@ -61,18 +69,23 @@ module ReactOnRailsHelper
61
69
 
62
70
  # Create the HTML rendering part
63
71
  server_rendered_html, console_script =
64
- server_rendered_react_component_html(options, props, react_component_name,
72
+ server_rendered_react_component_html(options, props_string, react_component_name,
65
73
  data_variable_name, dom_id)
66
74
 
75
+ content_tag_options = options.except(:generator_function, :prerender, :trace,
76
+ :replay_console, :id, :react_component_name,
77
+ :server_side)
78
+ content_tag_options[:id] = dom_id
79
+
67
80
  rendered_output = content_tag(:div,
68
81
  server_rendered_html,
69
- id: dom_id)
82
+ content_tag_options)
70
83
 
71
84
  # IMPORTANT: Ensure that we mark string as html_safe to avoid escaping.
72
85
  <<-HTML.html_safe
73
86
  #{data_from_server_script_tag}
74
87
  #{rendered_output}
75
- #{console_script}
88
+ #{replay_console(options) ? console_script : ""}
76
89
  HTML
77
90
  end
78
91
 
@@ -82,12 +95,12 @@ module ReactOnRailsHelper
82
95
  end
83
96
 
84
97
  # Returns Array [0]: html, [1]: script to console log
85
- def server_rendered_react_component_html(options, props, react_component_name, data_variable, dom_id)
98
+ def server_rendered_react_component_html(options, props_string, react_component_name, data_variable, dom_id)
86
99
  if prerender(options)
87
100
  render_js_expression = <<-JS
88
101
  (function(React) {
89
102
  #{debug_js(react_component_name, data_variable, dom_id, trace(options))}
90
- var reactElement = #{render_js_react_element(react_component_name, props.to_json, generator_function(options))}
103
+ var reactElement = #{render_js_react_element(react_component_name, props_string, generator_function(options))}
91
104
  return React.renderToString(reactElement);
92
105
  })(this.React);
93
106
  JS
@@ -135,6 +148,10 @@ module ReactOnRailsHelper
135
148
  options.fetch(:prerender) { ReactOnRails.configuration.prerender }
136
149
  end
137
150
 
151
+ def replay_console(options)
152
+ options.fetch(:replay_console) { ReactOnRails.configuration.replay_console }
153
+ end
154
+
138
155
  def debug_js(react_component_name, data_variable, dom_id, trace)
139
156
  if trace
140
157
  "console.log(\"RENDERED #{react_component_name} with data_variable"\
@@ -8,16 +8,18 @@ module ReactOnRails
8
8
  server_bundle_js_file: "app/assets/javascripts/generated/server.js",
9
9
  prerender: false,
10
10
  replay_console: true,
11
+ logging_on_server: true,
11
12
  generator_function: false,
12
- trace: Rails.env.development?
13
+ trace: Rails.env.development?,
13
14
  )
14
15
  end
15
16
 
16
17
  class Configuration
17
- attr_accessor :server_bundle_js_file, :prerender, :replay_console, :generator_function, :trace
18
+ attr_accessor :server_bundle_js_file, :prerender, :replay_console, :generator_function, :trace,
19
+ :logging_on_server
18
20
 
19
21
  def initialize(server_bundle_js_file: nil, prerender: nil, replay_console: nil,
20
- generator_function: nil, trace: nil)
22
+ generator_function: nil, trace: nil, logging_on_server: nil)
21
23
  if File.exist?(server_bundle_js_file)
22
24
  self.server_bundle_js_file = server_bundle_js_file
23
25
  else
@@ -26,6 +28,7 @@ module ReactOnRails
26
28
 
27
29
  self.prerender = prerender
28
30
  self.replay_console = replay_console
31
+ self.logging_on_server = logging_on_server
29
32
  self.generator_function = generator_function
30
33
  self.trace = trace.nil? ? Rails.env.development? : trace
31
34
  end
@@ -76,7 +76,20 @@ var console = { history: [] };
76
76
  json_string = ExecJS.eval(js_code_wrapper)
77
77
  end
78
78
  # element 0 is the html, element 1 is the script tag for the server console output
79
- JSON.parse(json_string)
79
+ result = JSON.parse(json_string)
80
+ if ReactOnRails.configuration.logging_on_server
81
+ console_script = result[1]
82
+ console_script_lines = console_script.split("\n")
83
+ console_script_lines = console_script_lines[2..-2]
84
+ re = /console\.log\.apply\(console, \["\[SERVER\] (?<msg>.*)"\]\);/
85
+ console_script_lines.each do |line|
86
+ match = re.match(line)
87
+ if match
88
+ Rails.logger.info { "[react_on_rails] #{match[:msg]}"}
89
+ end
90
+ end
91
+ end
92
+ return result
80
93
  end
81
94
 
82
95
  def self.wrap_code_with_exception_handler(js_code, component_name)
@@ -134,7 +147,7 @@ var console = { history: [] };
134
147
  end
135
148
 
136
149
  def console_replay_js_code
137
- @replay_console ? CONSOLE_REPLAY : ""
150
+ (@replay_console || ReactOnRails.configuration.logging_on_server) ? CONSOLE_REPLAY : ""
138
151
  end
139
152
 
140
153
  def base_js_code(bundle_js_code)
@@ -1,3 +1,3 @@
1
1
  module ReactOnRails
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: react_on_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Gordon