Class: React::ServerRendering::BundleRenderer

Inherits:
ExecJSRenderer show all
Defined in:
lib/react/server_rendering/bundle_renderer.rb

Overview

Extends ExecJSRenderer for the Rails environment

  • fetches JS code from the Rails app (webpacker or sprockets)

  • stringifies props

  • implements console replay

Constant Summary collapse

CONSOLE_POLYFILL =

Reimplement console methods for replaying on the client

File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/console_polyfill.js'))
CONSOLE_REPLAY =
File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/console_replay.js'))
CONSOLE_RESET =
File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/console_reset.js'))
TIMEOUT_POLYFILL =
File.read(File.join(File.dirname(__FILE__), 'bundle_renderer/timeout_polyfill.js'))

Constants inherited from ExecJSRenderer

ExecJSRenderer::GLOBAL_WRAPPER

Class Attribute Summary collapse

Attributes inherited from ExecJSRenderer

#context

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BundleRenderer

Returns a new instance of BundleRenderer



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/react/server_rendering/bundle_renderer.rb', line 19

def initialize(options={})
  @replay_console = options.fetch(:replay_console, true)
  filenames = options.fetch(:files, ['server_rendering.js'])
  js_code = CONSOLE_POLYFILL.dup
  js_code << TIMEOUT_POLYFILL.dup
  js_code << options.fetch(:code, '')

  filenames.each do |filename|
    js_code << asset_container.find_asset(filename)
  end

  super(options.merge(code: js_code))
end

Class Attribute Details

.asset_container_classObject

Returns the value of attribute asset_container_class



52
53
54
# File 'lib/react/server_rendering/bundle_renderer.rb', line 52

def asset_container_class
  @asset_container_class
end

Instance Method Details

#after_render(component_name, props, prerender_options) ⇒ Object



47
48
49
# File 'lib/react/server_rendering/bundle_renderer.rb', line 47

def after_render(component_name, props, prerender_options)
  @replay_console ? CONSOLE_REPLAY : ''
end

#asset_container#find_asset(logical_path)

Get an object which exposes assets by their logical path.

Out of the box, it supports a Sprockets::Environment (application.assets) and a Sprockets::Manifest (application.assets_manifest), which covers the default Rails setups.

You can provide a custom asset container with `React::ServerRendering::BundleRenderer.asset_container_class = MyAssetContainer`.

Returns:

  • (#find_asset(logical_path))

    An object that returns asset contents by logical path



65
66
67
# File 'lib/react/server_rendering/bundle_renderer.rb', line 65

def asset_container
  @asset_container ||= asset_container_class.new
end

#before_render(component_name, props, prerender_options) ⇒ Object



43
44
45
# File 'lib/react/server_rendering/bundle_renderer.rb', line 43

def before_render(component_name, props, prerender_options)
  @replay_console ? CONSOLE_RESET : ''
end

#render(component_name, props, prerender_options) ⇒ Object

Prerender options are expected to be a Hash however might also be a symbol. pass prerender: :static to use renderToStaticMarkup pass prerender: true to enable default prerender pass prerender: {} to proxy some custom options



37
38
39
40
41
# File 'lib/react/server_rendering/bundle_renderer.rb', line 37

def render(component_name, props, prerender_options)
  t_options = prepare_options(prerender_options)
  t_props = prepare_props(props)
  super(component_name, t_props, t_options)
end