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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/isomorfeus/preact_view_helper.rb', line 19
def mount_component(component_name, props = {}, asset_key = 'ssr.js', skip_ssr: false, use_ssr: false)
@ssr_response_status = nil
@ssr_styles = nil
thread_id_asset = "#{Thread.current.object_id}#{asset_key}"
render_result = "<div data-iso-env=\"#{Isomorfeus.env}\" data-iso-root=\"#{component_name}\" data-iso-props='#{Oj.dump(props, mode: :strict)}'"
if !skip_ssr && (Isomorfeus.server_side_rendering || use_ssr)
if Isomorfeus.development?
if Isomorfeus.ssr_contexts.key?(thread_id_asset)
uuid = Isomorfeus.ssr_contexts[thread_id_asset].instance_variable_get(:@uuid)
runtime = Isomorfeus.ssr_contexts[thread_id_asset].instance_variable_get(:@runtime)
runtime.vm.delete_context(uuid)
end
begin
init_speednode_context(asset_key, thread_id_asset)
rescue Exception => e
Isomorfeus.raise_error(message: "Server Side Rendering: Failed creating context for #{asset_key}. Error: #{e.message}", stack: e.backtrace)
end
else
unless Isomorfeus.ssr_contexts.key?(thread_id_asset)
init_speednode_context(asset_key, thread_id_asset)
end
end
ws_scheme = props[:location_scheme] == 'https:' ? 'wss:' : 'ws:'
location_host = props[:location_host] ? props[:location_host] : 'localhost'
api_ws_path = Isomorfeus.respond_to?(:api_websocket_path) ? Isomorfeus.api_websocket_path : ''
transport_ws_url = ws_scheme + location_host + api_ws_path
javascript = <<~JAVASCRIPT
global.Opal.Preact.render_buffer = [];
global.Opal.Preact.active_components = [];
global.Opal.Preact.active_redux_components = [];
global.FirstPassFinished = false;
global.Exception = false;
global.IsomorfeusSessionId = '#{Thread.current[:isomorfeus_session_id]}';
global.Opal.Isomorfeus['$env=']('#{Isomorfeus.env}');
if (typeof global.Opal.Isomorfeus.$negotiated_locale === 'function') {
global.Opal.Isomorfeus["$negotiated_locale="]('#{props[:locale]}');
}
global.Opal.Isomorfeus['$force_init!']();
global.Opal.Isomorfeus['$ssr_response_status='](200);
global.Opal.Isomorfeus.TopLevel['$ssr_route_path=']('#{props[:location]}');
let api_ws_path = '#{api_ws_path}';
let exception;
if (typeof global.Opal.Isomorfeus.Transport !== 'undefined' && api_ws_path !== '') {
global.Opal.Isomorfeus.TopLevel["$transport_ws_url="]("#{transport_ws_url}");
global.Opal.send(global.Opal.Isomorfeus.Transport.$promise_connect(), 'then', [], ($$1 = function(){
try {
global.Opal.Isomorfeus.TopLevel.$render_component_to_string('#{component_name}', #{Oj.dump(props, mode: :strict)});
global.FirstPassFinished = 'transport';
} catch (e) {
global.Exception = e;
global.FirstPassFinished = 'transport';
}
}, $$1.$$s = this, $$1.$$arity = 0, $$1))
} else { return global.FirstPassFinished = true; };
JAVASCRIPT
begin
first_pass_skipped = Isomorfeus.ssr_contexts[thread_id_asset].exec(javascript)
rescue Exception => e
Isomorfeus.raise_error(error: e)
end
unless first_pass_skipped
first_pass_finished, exception = Isomorfeus.ssr_contexts[thread_id_asset].exec('return [global.FirstPassFinished, global.Exception ? { message: global.Exception.message, stack: global.Exception.stack } : false ]')
Isomorfeus.raise_error(message: "Server Side Rendering: #{exception['message']}", stack: exception['stack']) if exception
unless first_pass_finished
start_time = Time.now
while !first_pass_finished
break if (Time.now - start_time) > 10
sleep 0.01
first_pass_finished = Isomorfeus.ssr_contexts[thread_id_asset].exec('return global.FirstPassFinished')
end
end
if first_pass_finished == 'transport'
transport_busy = Isomorfeus.ssr_contexts[thread_id_asset].exec('return global.Opal.Isomorfeus.Transport["$busy?"]()')
if transport_busy
start_time = Time.now
while transport_busy
break if (Time.now - start_time) > 10
sleep 0.01
transport_busy = Isomorfeus.ssr_contexts[thread_id_asset].exec('return global.Opal.Isomorfeus.Transport["$busy?"]()')
end
end
end
end
javascript = <<~JAVASCRIPT
global.Opal.Preact.render_buffer = [];
global.Opal.Preact.active_components = [];
global.Opal.Preact.active_redux_components = [];
global.Exception = false;
let rendered_tree;
let ssr_styles;
let component;
try {
rendered_tree = global.Opal.Isomorfeus.TopLevel.$render_component_to_string('#{component_name}', #{Oj.dump(props, mode: :strict)});
} catch (e) {
global.Exception = e;
}
let application_state = global.Opal.Isomorfeus.store.native.getState();
if (typeof global.Opal.Isomorfeus.Transport !== 'undefined') { global.Opal.Isomorfeus.Transport.$disconnect(); }
if (typeof global.NanoCSSInstance !== 'undefined') { ssr_styles = global.NanoCSSInstance.raw }
return [rendered_tree, application_state, ssr_styles, global.Opal.Isomorfeus['$ssr_response_status'](), global.Exception ? { message: global.Exception.message, stack: global.Exception.stack } : false];
JAVASCRIPT
rendered_tree, application_state, @ssr_styles, @ssr_response_status, exception = Isomorfeus.ssr_contexts[thread_id_asset].exec(javascript)
Isomorfeus.raise_error(message: exception['message'], stack: exception['stack']) if exception
render_result << " data-iso-hydrated='true'" if rendered_tree
if Isomorfeus.respond_to?(:current_user) && Isomorfeus.current_user && !Isomorfeus.current_user.anonymous?
render_result << " data-iso-usid=#{Oj.dump(Isomorfeus.current_user.to_sid, mode: :strict)}"
end
render_result << " data-iso-nloc='#{props[:locale]}'>"
render_result << (rendered_tree ? rendered_tree : "SSR didn't work")
else
if Isomorfeus.respond_to?(:current_user) && Isomorfeus.current_user && !Isomorfeus.current_user.anonymous?
render_result << " data-iso-usid=#{Oj.dump(Isomorfeus.current_user.to_sid, mode: :strict)}"
end
render_result << " data-iso-nloc='#{props[:locale]}'>"
end
render_result << '</div>'
if Isomorfeus.server_side_rendering
render_result = "<script type='application/javascript'>\nServerSideRenderingStateJSON = #{Oj.dump(application_state, mode: :strict)}\n</script>\n" << render_result
end
render_result
end
|