class ReactOnRails::ServerRenderingPool::RubyEmbeddedJavaScript
def exec_server_render_js(js_code, render_options, js_evaluator = nil)
Calling code will probably call 'html_safe' on return value before rendering to the view.
js_code MUST RETURN json stringify Object
Note, js_code does not have to be based on React.
hasErrors: true if server rendering errors
consoleReplayScript: script for replaying console
html: string of HTML for direct insertion on the page by evaluating js_code
Returns a Hash:
logging_on_server: put on server logs, not just in browser console
trace: saves the executed JS to a file, used in development
Using these options:
render_options: lib/react_on_rails/react_component/render_options.rb
js_code: JavaScript expression that returns a string.
def exec_server_render_js(js_code, render_options, js_evaluator = nil) js_evaluator ||= self if render_options.trace @file_index ||= 1 trace_js_code_used("Evaluating code to server render.", js_code, "tmp/server-generated-#{@file_index % 10}.js") @file_index += 1 end begin result = if render_options.streaming? js_evaluator.eval_streaming_js(js_code, render_options) else js_evaluator.eval_js(js_code, render_options) end rescue StandardError => err msg = <<~MSG Error evaluating server bundle. Check your webpack configuration. =============================================================== Caught error: #{err.message} =============================================================== MSG if err.message.include?("ReferenceError: self is not defined") msg << "\nError indicates that you may have code-splitting incorrectly enabled.\n" end msg << "\n#{Utils.default_troubleshooting_section}\n" raise ReactOnRails::Error, msg, err.backtrace end return parse_result_and_replay_console_messages(result, render_options) unless render_options.streaming? # Streamed component is returned as stream of strings. # We need to parse each chunk and replay the console messages. result.transform { |chunk| parse_result_and_replay_console_messages(chunk, render_options) } end