-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact.rb
63 lines (52 loc) · 1.46 KB
/
react.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Rails.configuration.react.server_renderer =
Class.new(React::ServerRendering::BundleRenderer) do
def before_render(component_name, props, prerender_options)
loadable_stats = Webpacker.config.public_output_path.join('client-loadable-stats.json').read
<<-JS
let manifest = #{loadable_stats};
JS
end
private
def render_from_parts(before, main, after)
js_code = compose_js(before, main, after)
@context.eval(js_code)
end
def main_render(component_name, props, prerender_options)
render_function = prerender_options.fetch(:render_function, 'renderToString')
"this.ReactRailsUJS.serverRender('#{render_function}', '#{component_name}', #{props}, manifest)"
end
def compose_js(before, main, after)
<<-JS
(function () {
#{before}
let data = #{main};
let result = data['html'];
#{after}
return data;
})()
JS
end
end
Rails.configuration.react.view_helper_implementation =
Class.new(React::Rails::ComponentMount) do
def setup(*)
super.tap { init_head_tags }
end
private
def prerender_component(*)
data = super
case data
when Hash
register_head_tags(data['headTags'].html_safe)
data['html'].html_safe
else
data.html_safe
end
end
def init_head_tags
@controller.instance_variable_set(:@react_head_tags, '')
end
def register_head_tags(tags)
@controller.instance_variable_get(:@react_head_tags) << tags.to_s
end
end