Skip to content

Commit df6ceef

Browse files
committed
refactor(SprocketsRenderer) don't use SetupJavascript
1 parent 298f23b commit df6ceef

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

lib/react/server_rendering/sprockets_renderer.rb

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ def initialize(options={})
55
@replay_console = options.fetch(:replay_console, true)
66

77
filenames = options.fetch(:files, ["react.js", "components.js"])
8-
js_code = SetupJavascript.new(filenames).code
8+
js_code = GLOBAL_WRAPPER + CONSOLE_POLYFILL
9+
10+
filenames.each do |filename|
11+
js_code << ::Rails.application.assets[filename].to_s
12+
end
13+
914
@context = ExecJS.compile(js_code)
1015
end
1116

1217
def render(component_name, props)
1318
# pass prerender: :static to use renderToStaticMarkup
14-
react_render_method = "renderToString"
1519
if props.is_a?(Hash) && props[:prerender] == :static
1620
react_render_method = "renderToStaticMarkup"
21+
else
22+
react_render_method = "renderToString"
1723
end
1824

1925
if !props.is_a?(String)
@@ -33,23 +39,14 @@ def render(component_name, props)
3339
raise PrerenderError.new(component_name, props, err)
3440
end
3541

36-
class SetupJavascript
37-
GLOBAL_WRAPPER = <<-JS
38-
var global = global || this;
39-
var self = self || this;
40-
var window = window || this;
41-
JS
42-
43-
attr_reader :code
44-
45-
def initialize(filenames)
46-
@code = GLOBAL_WRAPPER + CONSOLE_POLYFILL
47-
filenames.each do |filename|
48-
@code << ::Rails.application.assets[filename].to_s
49-
end
50-
end
51-
end
42+
# Handle node.js & other RubyRacer contexts
43+
GLOBAL_WRAPPER = <<-JS
44+
var global = global || this;
45+
var self = self || this;
46+
var window = window || this;
47+
JS
5248

49+
# Reimplement console methods for replaying on the client
5350
CONSOLE_POLYFILL = <<-JS
5451
var console = { history: [] };
5552
['error', 'log', 'info', 'warn'].forEach(function (fn) {
@@ -59,6 +56,7 @@ def initialize(filenames)
5956
});
6057
JS
6158

59+
# Replay message from console history
6260
CONSOLE_REPLAY = <<-JS
6361
(function (history) {
6462
if (history && history.length > 0) {

0 commit comments

Comments
 (0)