Class: React::ServerRendering::ExecJSRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/react/server_rendering/exec_js_renderer.rb

Overview

A bare-bones renderer for React.js + Exec.js

  • Depends on global ReactDOMServer in the ExecJS context

  • No Rails dependency

  • No browser concerns

Direct Known Subclasses

BundleRenderer

Constant Summary collapse

GLOBAL_WRAPPER =

Handle Node.js & other ExecJS contexts

<<-JS
  var global = global || this;
  var self = self || this;
JS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ExecJSRenderer

Returns a new instance of ExecJSRenderer.



11
12
13
14
# File 'lib/react/server_rendering/exec_js_renderer.rb', line 11

def initialize(options={})
  js_code = options[:code] || raise('Pass `code:` option to instantiate a JS context!')
  @context = ExecJS.compile(GLOBAL_WRAPPER + js_code)
end

Instance Attribute Details

#contextExecJS::Runtime::Context (readonly)

Returns The JS context for this renderer.

Returns:

  • (ExecJS::Runtime::Context)

    The JS context for this renderer



9
10
11
# File 'lib/react/server_rendering/exec_js_renderer.rb', line 9

def context
  @context
end

Instance Method Details

#after_render(component_name, props, prerender_options) ⇒ Object



27
# File 'lib/react/server_rendering/exec_js_renderer.rb', line 27

def after_render(component_name, props, prerender_options); ''; end

#before_render(component_name, props, prerender_options) ⇒ Object

Hooks for inserting JS before/after rendering



26
# File 'lib/react/server_rendering/exec_js_renderer.rb', line 26

def before_render(component_name, props, prerender_options); ''; end

#render(component_name, props, prerender_options) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/react/server_rendering/exec_js_renderer.rb', line 16

def render(component_name, props, prerender_options)
  js_executed_before = before_render(component_name, props, prerender_options)
  js_executed_after = after_render(component_name, props, prerender_options)
  js_main_section = main_render(component_name, props, prerender_options)
  render_from_parts(js_executed_before, js_main_section, js_executed_after)
rescue ExecJS::ProgramError => err
  raise React::ServerRendering::PrerenderError.new(component_name, props, err)
end